-
Apr 18th, 2014, 01:05 PM
#1
Thread Starter
Fanatic Member
DataGridView Delete Rows with UserDeletingRow
I have a DataGridView and I want to Delete Rows with the UserDeletingRow event. I tried deleting the entire selection at once, and that works great, but the event is then called extra times for all of the other rows, so I tried deleting them 1 at a time and then clearing the selection. Also, I have an idea that adding a primary key may be required. The issue is that if the user selects non-consecutive rows it is tough to delete.
This code works perfect if they hit a button and the event runs just 1 time, but the user must click the 'delete' button manually.
vb Code:
Private Sub btnDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnDelete.Click
If MsgBox("Are you sure you want to delete " & questionList.SelectedRows().Count & " records?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
For Each theRow As DataGridViewRow In questionList.SelectedRows()
deleteQuestion(theRow.Cells(0).Value, False) '"QuestionNumber"
Next
End If
End Sub
This works a little bit, and is fired by the event, but it does not delete all of the rows if they are non-consecutive.
vb Code:
Private Sub questionList_UserDeletingRow(sender As Object, e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles questionList.UserDeletingRow
'TODO: We need to add a primary key system to our questions to properly delete :(
If questionList.SelectedRows.Count > 0 Then
If questionList.SelectedRows().Count = 1 OrElse MsgBox("Are you sure you want to delete " & questionList.SelectedRows().Count & " records?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
'For Each theRow As DataGridViewRow In questionList.SelectedRows()
deleteQuestion(questionList.SelectedRows(0).Cells(0).Value, False) '"QuestionNumber"
' Next
End If
questionList.ClearSelection()
frmStudy.calculateTimes()
End If
End Sub
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Apr 18th, 2014, 01:31 PM
#2
Re: DataGridView Delete Rows with UserDeletingRow
I'm not following what you're trying to accomplish exactly, but here's an example of deleting rows in a DGV when one or more are selected, via the KeyDown event:
Code:
Private Sub dgv_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgv.KeyDown
If e.KeyValue = 46 Then
Dim str As String
Dim j As Integer
Dim ro As New List(Of Integer)
For j = 0 To dgv.SelectedCells.Count - 1
ro.Add(dgv.SelectedCells(j).RowIndex)
Next
ro.Sort()
For j = ro.Count - 1 To 0 Step -1
dgv.Rows.RemoveAt(ro(j))
Application.DoEvents()
Next
End If
End Sub
-
Apr 19th, 2014, 04:00 AM
#3
Re: DataGridView Delete Rows with UserDeletingRow
You seem to be abusing that event. The idea is that the event is raised when the user tries to delete a row and you can then stop that deletion. You seem to be doing much more than that, which is wrong. The user is already deleting the rows if that event is raised so you don't need to do anything more to delete the rows. You just do nothing and the rows will be deleted. If it's not working then it's because you're breaking it. The following code allowed me to delete multiple, non-contiguous rows successfully and also cancel the deletion, with just a single prompt in each case:
Code:
Private selectedRowCount As Integer? = Nothing
Private cancelDeletion As Boolean = False
Private Sub DataGridView1_UserDeletingRow(sender As Object, e As DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
If Not Me.selectedRowCount.HasValue Then
'This is the first row being deleted.
Me.selectedRowCount = Me.DataGridView1.SelectedRows.Count
Me.cancelDeletion = (MessageBox.Show("Are you sure you want to delete the selected row(s)?",
"Confirm Deletion",
MessageBoxButtons.OKCancel) = DialogResult.Cancel)
End If
e.Cancel = Me.cancelDeletion
If Me.DataGridView1.SelectedRows.Count = 1 OrElse
e.Row.Index = Me.DataGridView1.SelectedRows.Cast(Of DataGridViewRow).Max(Function(r) r.Index) Then
'This is the last row being deleted.
Me.selectedRowCount = Nothing
Me.cancelDeletion = False
End If
End Sub
-
Apr 28th, 2015, 02:55 PM
#4
New Member
Re: DataGridView Delete Rows with UserDeletingRow
There may be a simpler solution. The problem is that the event, UserDeletingRow, is called for each row deleted. The identification of which row is contained in e.row. The count of the number of calls will be contained in DGV.SelectedRows.Count. In order to retain state throughout the set of calls, one must remember the answer to the question and how many calls have been processed. Hence, the code might look something like:
Dim cstRowCount As Integer = 0
Dim iRet As MsgBoxResult
Private Sub CstAllocation_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles CstAllocation.UserDeletingRow
If cstRowCount = 0 Then
iRet = MsgBox(My.Resources._MsgVerifyRemoveCost, MsgBoxStyle.YesNo, My.Resources._MsgVerifyHdrRemoveCost)
cstRowCount = CstAllocation.SelectedRows.Count
End If
cstRowCount -= 1
' Process the answer
If iRet = MsgBoxResult.Yes Then
e.Cancel = false
'
'
'
Else
e.cancel = True
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|