deleting a row in a datagrid
-
to delete a row in a datagrid, i highlight the row and press a delete command button. this also works if i highlight the row and press delete on the keyboard. However, now i wish to perform calculations on the click of the delete command button. if delete on the keyboard is pressed, is there anyway to call the delete procedure? Thanks in advance!
-
to delete a row in a datagrid, i highlight the row and press a delete command button. this also works if i highlight the row and press delete on the keyboard. However, now i wish to perform calculations on the click of the delete command button. if delete on the keyboard is pressed, is there anyway to call the delete procedure? Thanks in advance!
If I'm following you correctly, what you're wanting to do is capture the delete keypress for the datagrid. The only useful way I've found for doing this is to inherit your own datagrid and then override PreProcessMessage to check for the delete key. I use this to confirm deletion before actually carrying it out. The class looks something like this:
Public Class DataGridConfirm > Inherits DataGrid Public Overrides Function PreProcessMessage(ByRef msg As System.Windows.Forms.Message) As Boolean > > > Dim keyCode As Keys = CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys) ' If this is a "KeyDown" message and the key pressed was "Delete" ' then we need to verify that the user actually wants to delete ' the record by presenting a messagebox ' Win32.Msg.WM_KEYDOWN just points to an Enum: WM_KEYDOWN=&H100 If msg.Msg = Win32.Msg.WM_KEYDOWN And keyCode = Keys.Delete Then > > > > > If ((MessageBox.Show("Are you sure you want to delete?", "AppTitle", _ MessageBoxButtons.YesNo)) = DialogResult.No) Then ' They've selected no, so simply return Return True End If > > > > End If ' If the user has not canceled the request, flow into the normal ' message processing of the base control Return MyBase.PreProcessMessage(msg) > > End Function End Class
After creating this class, just substitute it for the regular DataGrid your currently using.
Hope that helps!