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.

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each row As DataGridViewRow In DataGridView1.Rows
            If row.Cells("Delete").Value = True Then
                DataGridView1.Rows.Remove(row)
             End If
        Next
    End Sub

It worked with me..

'...
for each m_row as System.Windows.Forms.DataGridViewRow in me.DataGridView1.Rows
    if m_row.Cells("Column1").Value=true then
        me.DataGridView1.Rows.Remove(m_row)
    end if
next
'...

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

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

I wish that help you!

David Lyons Garcia

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.