locked
Deleting Rows From DataGridView - Selected CheckBox RRS feed

  • Question

  • Hello Experts,

    I am trying to delete certain rows from the datagridview with below code. I have a checkbox column and if user selects the checkbox then only that row should be deleted.

    For Each row As DataGridViewRow In dgvCIs.Rows
                        If row.Cells("checkbox").Value = True Then
                            dgvCIs.Rows.Remove(row)
                        End If
                    Next

    This code is working fine but only issue is that it is leaving one row as it is. Not deleting one row from the list of checked rows.

    Can anybody help me to find out what I am missing in this code?

    • Moved by Amanda Zhu Friday, October 31, 2014 10:06 AM
    Thursday, October 30, 2014 9:19 PM

Answers

  • Hello Experts,

    I am trying to delete certain rows from the datagridview with below code. I have a checkbox column and if user selects the checkbox then only that row should be deleted.

    For Each row As DataGridViewRow In dgvCIs.Rows
                        If row.Cells("checkbox").Value = True Then
                            dgvCIs.Rows.Remove(row)
                        End If
                    Next

    This code is working fine but only issue is that it is leaving one row as it is. Not deleting one row from the list of checked rows.

    Can anybody help me to find out what I am missing in this code?

    Hello James,

    You could use the following way instead.

      Dim lst As New List(Of DataGridViewRow)
            For Each row As DataGridViewRow In dgvCIs.Rows
                If row.Cells("checkbox").Value Then
                    If Not lst.Contains(row) Then
                        lst.Add(row)
                    End If
                End If
            Next
            For Each item As DataGridViewRow In lst
                dgvCIs.Rows.Remove(item)
            Next

    Or use this way.

     Dim i As Integer = 0
            Dim rCount As Integer = dgvCIs.Rows.Count - 1
            While i <= rCount
                If dgvCIs.Rows(i).Cells("checkbox").Value Then
                    dgvCIs.Rows.RemoveAt(i)
                    rCount -= 1               
                Else
                    i += 1
                End If
            End While

    Regards.

    Carl


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Carl Cai Friday, October 31, 2014 11:19 AM
    • Marked as answer by Carl Cai Sunday, November 9, 2014 1:08 PM
    Friday, October 31, 2014 10:38 AM
  • Hello,

    We need to use a for-next in reverse

    For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
        If CBool(DataGridView1.Rows(i).Cells("Checked").Value) Then
            DataGridView1.Rows.RemoveAt(i)
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Marked as answer by Carl Cai Sunday, November 9, 2014 1:08 PM
    Friday, October 31, 2014 12:01 PM
  • Hello,

    Did not look at the link but safe to say the reason for the current code not working is that when invoking the removal from a ContextMenu the DataGridView is still in edit mode while using a button the DataGridView is not in edit more so we need to end edit when the removal is called from the ContextMenu

    ''' <summary>
    ''' Two DataGridView columns
    ''' 0 CheckBox column named Checked
    ''' 1 TextBox column Column1
    ''' </summary>
    ''' <remarks></remarks>
    Public Class DeleteOnCheckedForm
    
        Private Sub DeleteToolStripMenuItem_Click(
            sender As Object,
            e As EventArgs) Handles DeleteToolStripMenuItem.Click
    
            DeleteCheckedRows(True)
    
        End Sub
        Private Sub Button1_Click(
            sender As Object, e As EventArgs) Handles Button1.Click
    
            DeleteCheckedRows(False)
    
        End Sub
        ''' <summary>
        ''' </summary>
        ''' <param name="Flag">True if invoked from contextmenu as without
        ''' it the DataGridView is still in edit mode
        ''' </param>
        ''' <remarks></remarks>
        Private Sub DeleteCheckedRows(ByVal Flag As Boolean)
    
            If Flag Then
                DataGridView1.EndEdit()
            End If
    
            For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
                If CBool(DataGridView1.Rows(i).Cells("Checked").Value) = True Then
                    Console.WriteLine(DataGridView1.Rows(i).Cells(1).Value)
                    DataGridView1.Rows.RemoveAt(i)
                End If
            Next
        End Sub
        Private Sub Button2_Click(
            sender As Object, e As EventArgs) Handles Button2.Click
    
            LoadData()
    
        End Sub
        Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
            LoadData()
        End Sub
        Private Sub LoadData()
            DataGridView1.Rows.Clear()
            DataGridView1.Rows.Add(New Object() {True, "John"})
            DataGridView1.Rows.Add(New Object() {True, "Mary"})
            DataGridView1.Rows.Add(New Object() {True, "Karen"})
            DataGridView1.Rows.Add(New Object() {True, "Bob"})
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Proposed as answer by Carl Cai Monday, November 3, 2014 1:59 AM
    • Marked as answer by Carl Cai Sunday, November 9, 2014 1:04 PM
    Friday, October 31, 2014 7:15 PM

All replies

  • Hi James_Smith,

    I moved this thread to Visual Basic forum for better support because your issue is on VB code.

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 31, 2014 10:07 AM
  • Hello Experts,

    I am trying to delete certain rows from the datagridview with below code. I have a checkbox column and if user selects the checkbox then only that row should be deleted.

    For Each row As DataGridViewRow In dgvCIs.Rows
                        If row.Cells("checkbox").Value = True Then
                            dgvCIs.Rows.Remove(row)
                        End If
                    Next

    This code is working fine but only issue is that it is leaving one row as it is. Not deleting one row from the list of checked rows.

    Can anybody help me to find out what I am missing in this code?

    Hello James,

    You could use the following way instead.

      Dim lst As New List(Of DataGridViewRow)
            For Each row As DataGridViewRow In dgvCIs.Rows
                If row.Cells("checkbox").Value Then
                    If Not lst.Contains(row) Then
                        lst.Add(row)
                    End If
                End If
            Next
            For Each item As DataGridViewRow In lst
                dgvCIs.Rows.Remove(item)
            Next

    Or use this way.

     Dim i As Integer = 0
            Dim rCount As Integer = dgvCIs.Rows.Count - 1
            While i <= rCount
                If dgvCIs.Rows(i).Cells("checkbox").Value Then
                    dgvCIs.Rows.RemoveAt(i)
                    rCount -= 1               
                Else
                    i += 1
                End If
            End While

    Regards.

    Carl


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Carl Cai Friday, October 31, 2014 11:19 AM
    • Marked as answer by Carl Cai Sunday, November 9, 2014 1:08 PM
    Friday, October 31, 2014 10:38 AM
  • Hello,

    We need to use a for-next in reverse

    For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
        If CBool(DataGridView1.Rows(i).Cells("Checked").Value) Then
            DataGridView1.Rows.RemoveAt(i)
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Marked as answer by Carl Cai Sunday, November 9, 2014 1:08 PM
    Friday, October 31, 2014 12:01 PM
  • Hi All,

    I have tried all the methods and found a strange behavior. When I am trying to delete records from context menu attached to datagird, it is deleting all records except last selected record. If I delete records on a button click then it is deleting all the selected records as expected.

    Summary:
    Context Menu: Not working and behavior is strange
    Button Click: Working fine.

    Project Download Link: http://www.filedropper.com/windowsapplication16
    Friday, October 31, 2014 4:46 PM
  • Hello,

    Did not look at the link but safe to say the reason for the current code not working is that when invoking the removal from a ContextMenu the DataGridView is still in edit mode while using a button the DataGridView is not in edit more so we need to end edit when the removal is called from the ContextMenu

    ''' <summary>
    ''' Two DataGridView columns
    ''' 0 CheckBox column named Checked
    ''' 1 TextBox column Column1
    ''' </summary>
    ''' <remarks></remarks>
    Public Class DeleteOnCheckedForm
    
        Private Sub DeleteToolStripMenuItem_Click(
            sender As Object,
            e As EventArgs) Handles DeleteToolStripMenuItem.Click
    
            DeleteCheckedRows(True)
    
        End Sub
        Private Sub Button1_Click(
            sender As Object, e As EventArgs) Handles Button1.Click
    
            DeleteCheckedRows(False)
    
        End Sub
        ''' <summary>
        ''' </summary>
        ''' <param name="Flag">True if invoked from contextmenu as without
        ''' it the DataGridView is still in edit mode
        ''' </param>
        ''' <remarks></remarks>
        Private Sub DeleteCheckedRows(ByVal Flag As Boolean)
    
            If Flag Then
                DataGridView1.EndEdit()
            End If
    
            For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
                If CBool(DataGridView1.Rows(i).Cells("Checked").Value) = True Then
                    Console.WriteLine(DataGridView1.Rows(i).Cells(1).Value)
                    DataGridView1.Rows.RemoveAt(i)
                End If
            Next
        End Sub
        Private Sub Button2_Click(
            sender As Object, e As EventArgs) Handles Button2.Click
    
            LoadData()
    
        End Sub
        Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
            LoadData()
        End Sub
        Private Sub LoadData()
            DataGridView1.Rows.Clear()
            DataGridView1.Rows.Add(New Object() {True, "John"})
            DataGridView1.Rows.Add(New Object() {True, "Mary"})
            DataGridView1.Rows.Add(New Object() {True, "Karen"})
            DataGridView1.Rows.Add(New Object() {True, "Bob"})
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Proposed as answer by Carl Cai Monday, November 3, 2014 1:59 AM
    • Marked as answer by Carl Cai Sunday, November 9, 2014 1:04 PM
    Friday, October 31, 2014 7:15 PM