Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone, I am very new to VB.net & don't know OOPS Concept Clearly.

I have a grid that has 3 columns, I want to Paste same column's previous row data on current cell when pressing some specific key(for ex:- F1)
See Grid Here

In the Above grid, First row "marks" column has value "Test", When I pressing F1(cursor in second row) I want to Paste "Test" to the second row('marks' column cell)

Reason to Implement because I need to Paste repeated same data on grid row


I tried below code its working but I am not sure which is right way

I copied
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean


From Stackoverflow

Please Explain above function(meaning of protected and override and its arguments) and I am using cell click event which is right or wrong

I Declared
Dim keyData, msg

But It shows warning message
"Variable 'msg' passed by reference before it has assigned a value. A null reference exception could result at run time"

and

"variable 'keydata' is used before it has assigned a value. A null reference exception could result at run time"


What I have tried:

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
      Dim keyData, msg
      Select Case DataGridView1.CurrentCell.ColumnIndex
          Case gcol.marks
              ProcessCmdKey(msg, keyData)
      End Select
  End Sub
  Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
      Select Case keyData
          Case Keys.F1
              Select Case Me.DataGridView1.CurrentCell.ColumnIndex
                  Case gcol.marks
                      Dim row = Me.DataGridView1.CurrentCell.RowIndex
                      With Me.DataGridView1
                          Dim tobepaste = .Item(gcol.marks, row - 1).Value
                          .CurrentCell.Value = tobepaste
                      End With
              End Select
              Return True
      End Select
      Return MyBase.ProcessCmdKey(msg, keyData)
  End Function
Posted
Updated 3-Jul-17 9:53am
Comments
Richard Deeming 1-Jun-17 9:19am    
What is the CellClick handler supposed to be doing, and what does it have to do with shortcut keys?
kumaran_ 2-Jun-17 0:57am    
See This link https://gyazo.com/1b6b2ae674b0580a4b37b8bb79f14cc2, want to paste 'Test' into 2 nd row's 2 column when pressing F1, see check line marked and cursor placed in the cell
Richard Deeming 2-Jun-17 6:52am    
And?

What do you think the CellClick event has to do with pressing a shortcut key?
kumaran_ 2-Jun-17 1:20am    
How to Use this for my case ?, Please Explain with example

1 solution

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean

It overrides Default Function
System.Windows.Forms.Form

overriding this function means Form can respond on KeyPress Event

simply you can use after this any key combinations to manipulate data which is associated with Form
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900