943,608 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 5911
  • VB.NET RSS
Apr 11th, 2009
0

Remove multiple rows in Datagrid

Expand Post »
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.

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rkckjk is offline Offline
2 posts
since Sep 2007
Apr 11th, 2009
0

Re: Remove multiple rows in Datagrid

It worked with me..
vb Syntax (Toggle Plain Text)
  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. '...
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Aug 9th, 2009
0

Re: Remove multiple rows in Datagrid

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 --:

vbnet Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davidlyons24 is offline Offline
1 posts
since Aug 2009
Oct 22nd, 2010
0

Delete multiple rows in DataGridView

David Lyons Garcia, thank you very much. You tell me the idea for opposite index.

In VB.NET the code seem to be:

Dim i As Integer = (DataGridView1.Rows.Count - 1)
Do While (i >= 0)
i = (i - 1)
For Each dr As DataGridViewRow In DataGridView1.Rows
If dr.Cells("check").Value = False Then
DataGridView1.Rows.Remove(dr)
End If
Next
Loop


The code is tested and work perfectly.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MegaSofijanov is offline Offline
1 posts
since Jan 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: help on updating datagriview
Next Thread in VB.NET Forum Timeline: Textbox Alignment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC