Why does my delete routine cause 2 rows to be removed from myDataGridView?

P

Patrick A

All,

My code (below) causes 2 rows disappear from the DataGridView when I
expect it to delete 1.

I'm going around circles...can someone please steer me in the right
direction?

Thanks,

In the following code,
clicking the Yes button makes 2 rows disappear from the DataGridView, (which I don't want) but only 1 delete from the underlying db (as desired).
clicking the No button makes 1 row disappear from the DataGridView, (which I don't want) with none deleting from the underlying db (as desired).
clicking the Cancel button has the same effect as clicking the No button.

Can anyone tell me what I've done wrong? In addition, if anyone knows
of a good working sample of code where you can delete all of the
"highlighted" rows from a DataGridView (if that's what you want) I'm
all ears! (Or eyes.)

Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
BindingNavigatorDeleteItem.Click
Dim myAnswer As System.Windows.Forms.DialogResult =
MessageBox.Show("Once you delete a button, it's gone. Delete it?",
"Delete?", MessageBoxButtons.YesNoCancel)
Select Case myAnswer
Case Windows.Forms.DialogResult.Yes
If Me.TBL_TimersDataGridView.SelectedRows.Count > 0
AndAlso _
Not
Me.TBL_TimersDataGridView.SelectedRows(0).Index = _
Me.TBL_TimersDataGridView.Rows.Count - 1 Then
Me.TBL_TimersDataGridView.Rows.RemoveAt( _
Me.TBL_TimersDataGridView.SelectedRows(0).Index)
Me.TBL_TimersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.L55TimerDataSet)
End If
Case Windows.Forms.DialogResult.No
Me.Focus()
Case Windows.Forms.DialogResult.Cancel
Me.Focus()
End Select
End Sub
 
P

Patrick A

Uh, nevermind, I forgot that the "autogenerated" code for the button
was still being run.

So I added my own button in it's place, and the following code, which
enables multi-row delete and asks the user if they are sure first.


Private Sub butMyDel_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles butMyDel.Click
Dim myAnswer As System.Windows.Forms.DialogResult =
MessageBox.Show("Once you delete a button, it's gone. Delete it?",
"Delete?", MessageBoxButtons.YesNoCancel)

Select Case myAnswer
Case Windows.Forms.DialogResult.Yes ' If the user clicks
the Yes button
'And at least 1 row is selected
If Me.TBL_TimersDataGridView.SelectedRows.Count > 0
AndAlso _
Not
Me.TBL_TimersDataGridView.SelectedRows(0).Index = _
Me.TBL_TimersDataGridView.Rows.Count - 1 Then '
And the row selected is not the new row row
For Each dgvRow As DataGridViewRow In
TBL_TimersDataGridView.SelectedRows 'For each selected row

Me.TBL_TimersDataGridView.Rows.RemoveAt(dgvRow.Index) 'Delete the
selected row
Next
Me.TBL_TimersBindingSource.EndEdit() 'And update
everything

Me.TableAdapterManager.UpdateAll(Me.L55TimerDataSet)
End If
Case Windows.Forms.DialogResult.No
Me.Focus()
Case Windows.Forms.DialogResult.Cancel
Me.Focus()
End Select

End Sub
 
Top