DataGridView and the Delete key

JackiTyan

Member
Joined
Oct 4, 2006
Messages
6
Programming Experience
Beginner
Hi, i have this idea of using the Del key (or any other key/key combo) to enable the user to delete the record for the currently selected cell.
I got this idea when i tried pressing different keys while the Datagridview was selected. The individual rows disappear after i press the Del key.

How can i detect if the user pressed the Del key, and warn user if he/she really want to physically delete said record?
Is there Datagridview property that handles this? Can somebody please give me an example code on how to do this.

Thanks in advance.

:)
 
There is an event that can handle this, the KeyDown event, which can also be cancelled by setting the e.Handled parameter property:
VB.NET:
Private Sub dgv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles dgv.KeyDown
    If e.KeyCode = Keys.Delete Then
        Dim title As String = "Confirm"
        Dim message As String = "really delete row?"
        Dim bt As MessageBoxButtons = MessageBoxButtons.OKCancel
        If MessageBox.Show(message, title, bt) = Windows.Forms.DialogResult.Cancel Then
            e.Handled = True
        End If
    End If
End Sub
 
Thanks for the response, i didn't know it is to be handled by the keydown event/(method?). Now i'll write my code and see how it goes.

Sorry i have posted in the wrong area of the forum, i thought this should be under Data Access. :)

Thank you for the help.
 
Back
Top