vendredi 25 août 2017

How to delete multiple rows from a datagridview by selecting the checkboxes in C#?

I have datagridview and header checkbox to check all the check boxes in the rows in the checkbox column (index of the column for all the check boxes 0). I have following code in the deleted button event:

for (int i = 0; i < datagridview.Rows.Count; i++)
            {
                if (Convert.ToBoolean(datagridview.Rows[i]
                                      .Cells[0].Value) == true)
                {
                    datagridview.Rows.RemoveAt(i);
                }

}

The datagridview.rows.count keeps decreasing as the row deleted but i in the loop keeps increasing. Thus, instead of deleting all the checked rows, it deletes some of them only. It does not delete the one with rows index 0, 1, 2 and so on with the new data in the datagridview as i increasing in the code.




Aucun commentaire:

Enregistrer un commentaire