0

Possible Duplicate:
how to delete row from datagridview with a delete button?

I have System.Windows.Forms.DataGrid. How to delete selected row by delete button click? Selected row, not when cursor in cell!!!!!

If I click delete key on selected row it removes row immediately. I want to show dialog box here to confirm.

Thanks!

Community
  • 1
  • 1
Radislav
  • 2,892
  • 7
  • 38
  • 61

1 Answers1

1

Here is the code for one that will raise an event to let you the user is about to delete a row.

Public Class ConfirmDeleteDataGrid

Inherits DataGrid

Public Event DeletedRow(ByVal sender As Object, ByVal e As EventArgs)

Private Const WM_KEYDOWN = &H100



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 msg.Msg = WM_KEYDOWN And keyCode = Keys.Delete Then

If MessageBox.Show("Delete This Row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

Return MyBase.PreProcessMessage(msg)

End Function

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean

Dim pt As Point

Dim hti As DataGrid.HitTestInfo

pt = Me.PointToClient(Cursor.Position)

hti = Me.HitTest(pt)

If keyData = Keys.Delete Then

If hti.Type = Me.HitTestType.RowHeader Then

If MessageBox.Show("Delete this row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

End If

Return MyBase.ProcessDialogKey(keyData)

End Function



Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseDown(e)

End Sub

Public Sub New()

Trace.WriteLine(Me.VertScrollBar.Visible.ToString)

End Sub

Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseMove(e)

End Sub


End Class
Brijesh Patel
  • 2,901
  • 15
  • 50
  • 73
  • System.Windows.Forms.DataGrid does not have userDeletingRow event :( – Radislav Mar 22 '12 at 13:12
  • Are you not using System.windows.Forms.DataGridView? – Brijesh Patel Mar 22 '12 at 13:20
  • No, I am have class which are inherited from System.Windows.Forms.DataGrid. If I click delete key on selected row it removes row immediately. I want to show dialog box here to confirm. – Radislav Mar 22 '12 at 13:21
  • I have edited my previous code just checked it. – Brijesh Patel Mar 22 '12 at 13:36
  • I did almost the same 10 minutes ago. The problem is next. Then you select the row and press delete PreProcessMessage is not invoked. But if you select cell, or move mouse to empty field in anywhere on form, this methos is called. I don't know why :( – Radislav Mar 22 '12 at 13:43
  • Oh, sorry, I missed some important think. Thank a lot for your help!!!! +100 – Radislav Mar 22 '12 at 13:56