Reputation: 614
I am trying to solve this good old problem in my own environment, adapted lots of different solutions, still no success.
I have a User Control
, named EntryGrid
, that has a DataGridView
, it's headers and such things are set in the code. Then there is a form that has an EntryGrid
dropped to it.
I know that excel columns must be prepared to be able to paste all cells into a row, for this I used this solution: copypaste, except the Copy part. This is how it looks like at my place:
Private Sub EntryGrid_KeyDown(sender As Object, e As KeyEventArgs) Handles EntryGrid4.KeyDown, EntryGrid8.KeyDown, EntryGrid16.KeyDown, EntryGrid32.KeyDown
e.Handled = True
Dim entryGrid As EntryGrid = sender
Dim dataGrid As DataGridView = entryGrid.DataGrid
If (e.Control And e.KeyCode = Keys.V) Then
MessageBox.Show("Success") 'for now
End If
End Sub
This is absolutely not working for me. I even set KeyPreview
to True
in the form, but nothing happens ever.
Then I tried this solution, mine with any result:
Private Sub EntryGrid_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles EntryGrid4.EditingControlShowing
AddHandler e.Control.KeyDown, AddressOf cell_KeyDown
End Sub
Private Sub cell_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.V Then
MessageBox.Show("Success")
End If
End Sub
EntryGrid4
is the name of the usercontrol on the form, but it hasn't got any EditingControlShowing
event, only DataGridView
has, but I cannot use like this: EntryGrid4.DataGrid.EditingControlShowing
I created an event in EntryGrid
(Public Event EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
), but anything's changed.
I am able to paste anything from clipboard to a cell, but it had been possible before I started the modifying.
Thanks for any idea!
Upvotes: 1
Views: 1916
Reputation: 125197
ProcessCmdKey
method of your UserControl
is what you are looking for. Override it and check for Ctrl + V and do what you need. For example:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.V))
{
if (dataGridView1.EditingControl != null)
dataGridView1.EditingControl.Text = Clipboard.GetText();
else if (dataGridView1.CurrentCell != null)
this.dataGridView1.CurrentCell.Value = Clipboard.GetText();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Upvotes: 3