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 check
Here is another example you can try
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView.Columns["Select"].Index)
{
dataGridView.EndEdit();
if ((bool)dataGridView.Rows[e.RowIndex].Cells["Select"].Value)
{
//-- checking current select, needs to uncheck any other cells that are checked
foreach(DataGridViewRow row in dataGridView.Rows)
{
if (row.Index == e.RowIndex)
{
dataGridView.Rows[row.Index].SetValues(true);
}
else
{
dataGridView.Rows[row.Index].SetValues(false);
}
}
}
}
}
All of the casting causes errors, nothing here I tried worked, so I fiddled around and got this to work.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value != null && (bool)row.Cells[0].Value)
{
Console.WriteLine(row.Cells[0].Value);
}
}
that worked for me after clearing selection, BeginEdit and change the girdview rows and end the Edit Mode.
if (dgvDetails.RowCount > 0)
{
dgvDetails.ClearSelection();
dgvDetails.BeginEdit(true);
foreach (DataGridViewRow dgvr in dgvDetails.Rows)
{
dgvr.Cells["cellName"].Value = true;
}
dgvDetails.EndEdit();
}
I use the CellMouseUp event. I check for the proper column
if (e.ColumnIndex == datagridview.Columns["columncheckbox"].Index)
I set the actual cell to a DataGridViewCheckBoxCell
dgvChkBxCell = datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
Then check to see if it's checked using EditingCellFormattedValue
if ((bool)dgvChkBxCell.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.
Try the below code it should work
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == false)
{
foreach (DataGridViewRow row in dGV1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
chk.Value = chk.TrueValue;
}
}
else if (checkBox2.Checked == true)
{
foreach (DataGridViewRow row in dGV1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
chk.Value = 1;
if (row.IsNewRow)
{
chk.Value = 0;
}
}
}
}
you can try this code:
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.CurrentRow.Cells[0];
dataGridView1.BeginEdit(true);
if (chk.Value == null || (int)chk.Value == 0)
{
chk.Value = 1;
}
else
{
chk.Value = 0;
}
dataGridView1.EndEdit();