Can someone help me why it doesn't work?
I have a checkbox
and if I click on it,
this should uncheck all the checkbox inside the datagridview which were checked before including the user selected checkbox.
Here is the code:
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;
}
}
}
the checkbox should not be selected. it should be checked.
here is the added column
DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
CheckBox chk = new CheckBox();
CheckboxColumn.Width = 20;
datagridview1.Columns.Add(CheckboxColumn);
Here is another example you can try
I use the CellMouseUp event. I check for the proper column
I set the actual cell to a DataGridViewCheckBoxCell
Then check to see if it's checked using EditingCellFormattedValue
You will have to check for keyboard entry using the KeyUp event and check the .value property and also check that the CurrentCell's column index matches the checkbox column. The method does not provide e.RowIndex or e.ColumnIndex.
I had the same problem, and even with the solutions provided here it did not work. The checkboxes would simply not change, their Value would remain null. It took me ages to realize my dumbness:
Turns out, I called the
form1.PopulateDataGridView(my data)
on the Form derived class Form1 before I calledform1.Show()
. When I changed up the order, that is to callShow()
first, and then read the data and fill in the checkboxes, the value did not stay null.