Results 1 to 4 of 4

Thread: DataGridView Delete Rows with UserDeletingRow

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Location
    Florida, USA
    Posts
    563

    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:
    1. Private Sub btnDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnDelete.Click
    2. If MsgBox("Are you sure you want to delete " & questionList.SelectedRows().Count & " records?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
    3.                             For Each theRow As DataGridViewRow In questionList.SelectedRows()
    4.                                 deleteQuestion(theRow.Cells(0).Value, False) '"QuestionNumber"
    5.                             Next
    6.                         End If
    7. 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:
    1. Private Sub questionList_UserDeletingRow(sender As Object, e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles questionList.UserDeletingRow
    2.         'TODO: We need to add a primary key system to our questions to properly delete :(
    3.         If questionList.SelectedRows.Count > 0 Then
    4.  
    5.             If questionList.SelectedRows().Count = 1 OrElse MsgBox("Are you sure you want to delete " & questionList.SelectedRows().Count & " records?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
    6.                 'For Each theRow As DataGridViewRow In questionList.SelectedRows()
    7.                 deleteQuestion(questionList.SelectedRows(0).Cells(0).Value, False) '"QuestionNumber"
    8.                 ' Next
    9.             End If
    10.             questionList.ClearSelection()
    11.             frmStudy.calculateTimes()
    12.         End If
    13.     End Sub
    I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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

  4. #4
    New Member
    Join Date
    Apr 2015
    Posts
    1

    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
  •  



Click Here to Expand Forum to Full Width