none
Datavridview bound to Datatable - loop to delete rows RRS feed

  • Question

  • hi

    I have a DataGridview(DGV)  bound to a DataTable (DT). In the DT I have a checkbox column that I show in DGV.

    I  can check the chekbox field for several rows and click a Button  to delete all checked rows

    I loop the DGV to control if a row is checked or not and then delete row in DT

    I use this code

    Dim pk As Integer
    Dim dtrow As DataRow
    Dim nrow As Integer
    
    For Each row As DataGridViewRow In DGV.Rows
    	
        If row.Cells("ck").Value = True Then
    	pk = row.Cells("NazioneId").Value
    	dtrow = UI_DT_Nazioni.Rows.Find(pk)
    	dtrow.Delete()	
        End If
    Next
    This code does not work. In fact if I check rows 1 2 3 in the DGV then, after deleting row 1 in DT, the DGV is updated so row 2 became row 1 in DGV and the original row 2 is not deleted.


    Which is the best way to do the right job. ?

    Thanks for help 

    Friday, March 23, 2018 8:18 PM

Answers

  • Hi

    Modifying the rows inside a loop causes that issue. To do this,I always 'reverse' loop through the rows. i.e.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("Freddy")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	With dt
    	  .Columns.Add("ck", GetType(Boolean))
    	  .Columns.Add("NazioneId", GetType(Integer))
    	  .Columns.Add("NazioneId2", GetType(String))
    	  .Columns.Add("NazioneId3", GetType(String))
    
    	  .Rows.Add(True, 1, "One", "Two")
    	  .Rows.Add(False, 1, "Three", "Four")
    	  .Rows.Add(True, 1, "Five", "Six")
    	  .Rows.Add(False, 1, "Seven", "Eight")
    	  .Rows.Add(True, 1, "Nine", "Ten")
    	End With
    	DataGridView1.DataSource = dt
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	For i As Integer = dt.Rows.Count - 1 To 0 Step -1
    	  If CBool(dt.Rows(i).Item("ck")) = True Then
    		dt.Rows(i).Delete()
    	  End If
    	Next
    	dt.AcceptChanges()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Claudio111 Saturday, March 24, 2018 7:25 AM
    Friday, March 23, 2018 8:53 PM

All replies

  • Hi

    Modifying the rows inside a loop causes that issue. To do this,I always 'reverse' loop through the rows. i.e.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("Freddy")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	With dt
    	  .Columns.Add("ck", GetType(Boolean))
    	  .Columns.Add("NazioneId", GetType(Integer))
    	  .Columns.Add("NazioneId2", GetType(String))
    	  .Columns.Add("NazioneId3", GetType(String))
    
    	  .Rows.Add(True, 1, "One", "Two")
    	  .Rows.Add(False, 1, "Three", "Four")
    	  .Rows.Add(True, 1, "Five", "Six")
    	  .Rows.Add(False, 1, "Seven", "Eight")
    	  .Rows.Add(True, 1, "Nine", "Ten")
    	End With
    	DataGridView1.DataSource = dt
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	For i As Integer = dt.Rows.Count - 1 To 0 Step -1
    	  If CBool(dt.Rows(i).Item("ck")) = True Then
    		dt.Rows(i).Delete()
    	  End If
    	Next
    	dt.AcceptChanges()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Claudio111 Saturday, March 24, 2018 7:25 AM
    Friday, March 23, 2018 8:53 PM
  • Thank you. Good solution 😅
    Friday, March 23, 2018 9:15 PM