The code you are trying here will flip the states (if true then became false vice versa) of the checkboxes irrespective of the user selected checkbox because here the foreach
is selecting each checkbox
and performing the operations.
To make it clear, store the index
of the user selected checkbox before performing the foreach
operation and after the foreach
operation call the checkbox by mentioning the stored index and check it (In your case, make it True
-- I think).
This is just logic and I am damn sure it is correct. I will try to implement some sample code if possible.
Modify your foreach
something like this:
//Store the index of the selected checkbox here as Integer (you can use e.RowIndex or e.ColumnIndex for it).
private void chkItems_CheckedChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in datagridview1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
if (chk.Selected == true)
{
chk.Selected = false;
}
else
{
chk.Selected = true;
}
}
}
//write the function for checking(making true) the user selected checkbox by calling the stored Index
The above function makes all the checkboxes true including the user selected CheckBox. I think this is what you want..