This turned out to be more fiddly than I expected!
Choosing the appropriate event to put code behind was the first problem. CellValidated or CellValueChanged were my first thoughts, but these were not being fired when I expected (for example CellValidated is not fired when the checkbox is clicked, but when you click elsewhere - not a good user experience)
I ended up choosing the
dataGridView2_CellClick
event, which brings it's own problems ... when checking the
.Value
of the cell on the first click you will find it is
null
. So I had to use
EditingCellFormattedValue
instead - trouble is, at the time the event is fired this value is still the
opposite of what we think it is ... in other words when we are clicking the tick box this value is still false - it has the value we are moving
from not the value we are moving
to. I'm labouring this point a little to draw attention to the not (
!
) operator in the line
var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;
The next issue to consider is whether or not the properties
AllowUserToAddRows
has been set to true or false (the default is true). In the code below I handle either, but if you omit the setting of this to false when using the
foreach
you will get an exception
Quote:
An unhandled exception of type 'System.NullReferenceException' occurred in Sandbox.exe
Additional information: Object reference not set to an instance of an object.
Here is my full solution
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
var r = e.RowIndex;
var c = e.ColumnIndex;
if (dataGridView2.Rows[r].Cells[c].GetType() != typeof(DataGridViewCheckBoxCell)) return;
var procodSearch = dataGridView2.Rows[r].Cells[2].Value.ToString();
var typcodSearch = dataGridView2.Rows[r].Cells[3].Value.ToString();
var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;
var temp = dataGridView1.AllowUserToAddRows;
dataGridView1.AllowUserToAddRows = false;
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (dr.Cells[dataGridView1.Columns["procod"].Index].Value.ToString().Equals(procodSearch)
&& dr.Cells[dataGridView1.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch))
((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;
}
dataGridView1.AllowUserToAddRows = temp;
temp = dataGridView3.AllowUserToAddRows;
dataGridView3.AllowUserToAddRows = false;
foreach (DataGridViewRow dr in dataGridView3.Rows)
{
if (dr.Cells[dataGridView3.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch))
((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;
}
dataGridView3.AllowUserToAddRows = temp;
}
Or if you prefer to use Linq:
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
var r = e.RowIndex;
var c = e.ColumnIndex;
if (dataGridView2.Rows[r].Cells[c].GetType() != typeof(DataGridViewCheckBoxCell)) return;
var procodSearch = dataGridView2.Rows[r].Cells["procod"].Value.ToString();
var typcodSearch = dataGridView2.Rows[r].Cells["typcod"].Value.ToString();
var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;
var temp = dataGridView1.AllowUserToAddRows;
dataGridView1.AllowUserToAddRows = false;
foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(dr => dr.Cells[dataGridView1.Columns["procod"].Index].Value.ToString().Equals(procodSearch)
&& dr.Cells[dataGridView1.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch)))
((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;
dataGridView1.AllowUserToAddRows = temp;
temp = dataGridView3.AllowUserToAddRows;
dataGridView3.AllowUserToAddRows = false;
foreach (DataGridViewRow dr in dataGridView3.Rows.Cast<DataGridViewRow>()
.Where(dr => dr.Cells[dataGridView3.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch)))
((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;
dataGridView3.AllowUserToAddRows = temp;
}
This is not an ideal solution - for example I have made some assumptions about which columns contain the CheckBoxes (I've assumed column 0 above) and there is no checking for null values in any columns, but it's a start.