monami monami - 1 year ago 132 Question

How to enable copy-paste from Excel into custom DataGridView?

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
, that has a
, it's headers and such things are set in the code. Then there is a form that has an
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
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
End If
End Sub

is the name of the usercontrol on the form, but it hasn't got any
event, only
has, but I cannot use like this:

I created an event in
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!

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download