I'm having a simple DataGridView that uses the table Orders(OrderID,CustomerID,ShipName) from the northwind database.After the initial load, i'm adding a new column to each row with a checkbox on it. Now my UI looks like this.
Code for adding the extra column with the checkbox.
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "Delete";
checkColumn.HeaderText = "Delete";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
dataGridView1.Columns.Add(checkColumn);
Also, i have a button called delete which on click ,should delete all rows with the checkbox ticked.
for (int i = dataGridView1.Rows.Count-1; i >=0 ; i--)
{
DataGridViewRow row = dataGridView1.Rows[i];
bool isChecked = dataGridView1[3, i].Value != null && (bool)dataGridView1[3, i].Value != false;
if (isChecked)
{
dataGridView1.Rows.RemoveAt(i);
}
}
However, everytime i click some checkboxes and then the button delete, the last one clicked always isn't deleted.
After some debugging , i found that if i click some checkboxes, then click anywhere else in the UI, and then on the button delete it works as intended.
So, is this a known bug? Am i doing something wrong?
UPDATE
Added a counter to the delete method so i can count how many checkboxes are ticked and procceed to another test:
1: I clicked on the first two checkboxes and then the button delete.The first row was deleted, the counter was 1.
2: I clicked on the first two checkboxes and then outside on a empty space of UI, and then the button delete.Both ticked rows were deleted.The counter was 2.
So the problem is that visually the checkboxes are ticked, but in the code the last one is not unless i change my focus.