Remove multiple rows in Datagrid

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 2
Reputation: rkckjk is an unknown quantity at this point 
Solved Threads: 0
rkckjk rkckjk is offline Offline
Newbie Poster

Remove multiple rows in Datagrid

 
0
  #1
Apr 11th, 2009
I have the following code in which it supposed to remove rows that have the first column checkboxes checked, but it only removes one row at a time.

If you have more than one checkbox checked, you have to click the command button each time to remove the row.

It seems that the logic is correct to remove multiple rows if more than one checkbox is checked, but it doesn't work.

  1.  
  2. Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. For Each row As DataGridViewRow In DataGridView1.Rows
  4. If row.Cells("Delete").Value = True Then
  5. DataGridView1.Rows.Remove(row)
  6. End If
  7. Next
  8. End Sub
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Remove multiple rows in Datagrid

 
0
  #2
Apr 11th, 2009
It worked with me..
  1. '...
  2. for each m_row as System.Windows.Forms.DataGridViewRow in me.DataGridView1.Rows
  3. if m_row.Cells("Column1").Value=true then
  4. me.DataGridView1.Rows.Remove(m_row)
  5. end if
  6. next
  7. '...
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: davidlyons24 is an unknown quantity at this point 
Solved Threads: 0
davidlyons24 davidlyons24 is offline Offline
Newbie Poster

Re: Remove multiple rows in Datagrid

 
0
  #3
Aug 9th, 2009
Hello,
The problem that you have is when you delete the row the datagrid reindexes it automatically so it can not acces correctly to the next row. So what you have to do is the opposite index counting with minus minus --:

  1. for (int i = (dgVentaActiva.Rows.Count - 1); i >= 0; i --)
  2. {
  3. if ((Int32)dgVentaActiva["id_master", i].Value == lVendaElements[idElementVenda].elementIDTemporal)
  4. dgVentaActiva.Rows.RemoveAt(i);
  5. }
  6. lVendaElements.RemoveAt(idElementVenda);

I wish that help you!

David Lyons Garcia
Last edited by John A; Aug 9th, 2009 at 6:26 pm. Reason: removed fake signature, added code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC