I have a datagridview with a binding navigator with a delete button (the red X). I want a message box to appear when the user clicks on the delete button to confirm whether they in fact want to delete the record. I have the following code so far:
Private
Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.ClickIf MsgBox("Are you sure you want to delete selected record(s) ", MessageBoxButtons.OKCancel, "Confirm Delete") = Windows.Forms.DialogResult.Cancel Then
End If
End Sub
As you can see, I do not have anything after the Then line because I can't figure out what code I need to cancel the event. Of course, there may be a better way to approach this.
I also would like to know how to handle the user selecting a record and hitting the delete button on their keyboard to delete a record so the same message box appears and cancels the delete if the user chooses Cancel.
Can anyone help
Thanks,
John

Confirm Deletion of a Record
NytewolfAU2k7
thats right - the surpress has nothing to do with the delete event itself. It's just a keystroke event. Now, you need to check to see if a record has been selected before deleting and to do this, check to see the SelectedRows property then if its more than 0 then show the message box dialog.
if Me.theDataGridView.SelectedRows.Count > 0 then
if e.KeyCode = Keys.Delete then
'show confirmation of delete
end if
end if
I'm trying to figure out how to cancel the deletion event however. I've not done this but read about it a while ago.
in the bindingnavigator, there is a property called DeleteItem If so, set this to (none) or nothing to cancel the event. Does this work
Burr Sutter
I solved the BindingNavigator problem (sort of). I went to the BindingNavigator properties in the design view of the form and changed the DeleteItem to None, rather than the default BindingNavitagorDeleteItem. Then I changed the code slightly to the following:
Private
Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click If MsgBox("Are you sure you want to delete selected record(s) ", MessageBoxButtons.OKCancel, "Confirm Delete") = Windows.Forms.DialogResult.OK ThenCatalogBindingSource.RemoveCurrent()
End If End SubThe reason I said "sort of" is that if I happen to select multiple records, the RemoveCurrent only seems to delete the first row selected, not all the selected rows. Is there a way to get all of them deleted Would it involve a For, Next routiene
I tried something similar for the delete key, but for some reason it doesn't work. So I'm still looking for a solution for cancelling the delete if the user chooses Cancel after the user presses the delete key.
John
Maciej Koper
in regards to the key board, you would need to implement a keydown event, check the key they pressed, check to see if they selected a row in the datagridview, then go on and confirm.
will think about the cancelling the deletion event in the bindingnavigator
Koray Samsun
for the delete key, I guess you would be better calling the BindingNavigatorDeleteItem_Click:
Me.BindingNavigatorDeleteItem_Click(Me.theTextBox, new EventArgs)
as for deleting multiple records, not entirely sure, you may need to do a for each loop and delete the items from the datagridview but no idea how it would work if you used the CatalogBindingSource.
hdp203
ahmedilyas:
I solved the problem I was having with the delete key. Rather than an If Then if the user selects OK, I changed it to Cancel and found code to cancel the keypress. Here is the code that I got to work:
Private
Sub CatalogDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CatalogDataGridView.KeyDownIf e.KeyCode = Keys.Delete And CatalogDataGridView.CurrentRow.Selected = True Then
If MsgBox("Are you sure you want to delete selected record(s) ", MessageBoxButtons.OKCancel, "Confirm Delete") = Windows.Forms.DialogResult.Cancel Then
e.SuppressKeyPress = True
Else
CatalogBindingSource.RemoveCurrent()
End If
End If
End Sub
Thanks for all your help!
John
Garrett - MSFT
Here is what I have come up with for the keydown event. I'm still struggling to find the right code to cancel the event if the user selects Cancel so the record does not get deleted.
Private
Sub CatalogDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CatalogDataGridView.KeyDown If e.KeyCode = Keys.Delete Then If MsgBox("Are you sure you want to delete selected record(s) ", MessageBoxButtons.OKCancel, "Confirm Delete") = Windows.Forms.DialogResult.Cancel Thene.SuppressKeyPress =
False End If End If End SubWhen I test this and click the Delete key, the message appears, but if I choose Cancel, the record still gets deleted. So obviously the 'e.SuppressKeyPress = False' line does not work.
Any suggestions
Thanks,
John
Prabu.
I've solved the deletion of multiple selected rows when using the BindingNavigatorDeleteItem. Here's what I used. This routine now works to delete a single row or multiple rows and cancels the deletion if the user selects Cancel from the messagebox.
Private
Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.ClickIf MsgBox("Are you sure you want to delete selected record(s) ", MessageBoxButtons.OKCancel, "Confirm Delete") = Windows.Forms.DialogResult.OK Then
If CatalogDataGridView.SelectedRows.Count > 1 Then
For Each SelectedRow As DataGridViewRow In CatalogDataGridView.SelectedRows
CatalogDataGridView.Rows.Remove(SelectedRow)
Next
Else
CatalogBindingSource.RemoveCurrent()
End If
End If
End Sub
I still need a solution to cancel the delete if the user clicks on the delete key but then selects Cancel from the messagebox. Again, here is what I have:
Private
Sub CatalogDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CatalogDataGridView.KeyDownIf e.KeyCode = Keys.Delete And CatalogDataGridView.CurrentRow.Selected = True Then
If MsgBox("Are you sure you want to delete selected record(s) ", MessageBoxButtons.OKCancel, "Confirm Delete") = Windows.Forms.DialogResult.OK Then
CatalogBindingSource.RemoveCurrent()
End If
End If
End Sub
Interesting enough, I do not need a For Each statement to delete multiple rows. This code will delete a single selected row or multiple selected rows. I just can't get the delete function to cancel if the user changes his mind about deleting.
Any thoughts
John
supagu
You're right about needing to check if a row is selected. I forgot that when I posted my reply. Here is my revised line. It's different than you suggested, but it works.
If
e.KeyCode = Keys.Delete And CatalogDataGridView.CurrentRow.Selected = True ThenThe messagebox will now only appear if a row or multiple rows have been selected.
I'll try your suggestion for the bindingnavigator. Let me know if you find anything else out.
Thanks.
John