Hi, it is because when you are deleting a row from a gridview the next row will take place of the deleted row.That means if you have stored the indexes of
checkboxes
which are
true
, after deleting a row now they have different indexes. I hope it is clear to you. So what you can do is keep a int type variable called
countDeletedRows
to keep how many rows you have deleted.Then use it as below to delete the records.
Note : i used
dataGridView1.Rows.RemoveAt()
to delete a datagrid row
Below code will delete all the rows which checkboxes are checked
private void button1_Click(object sender, EventArgs e)
{
List<object> ChkedRow = new List<object>();
int countDeletedRows = 0;
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true)
{
ChkedRow.Add(i);
}
}
foreach (int k in ChkedRow)
{
dataGridView1.Rows.RemoveAt(k - countDeletedRows);
countDeletedRows++;
}
}