You can do this by using the CellValueChanged
event like this:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// se a célula alterada for a célula de interesse (1.)
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("chk"))
{
bool marcado = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["chk"]; // (2.)
// se a célula estiver marcada
if ((bool)(chk.Value ?? false) == true) // (3.) e (4.)
{
marcado = true;
break;
}
}
if (marcado)
button1.Enabled = true;
else
button1.Enabled = false;
}
}
Here are a few things to keep in mind:
I used a if
before checking the values of DataGridViewCheckBoxCell
to prevent the check from occurring in all columns.
As I said in the comments, it was necessary to change dataGridView1.CurrentRow
to row
, otherwise your for
would not go through all the lines, but only the current line (referring to the changed cell).
The ??
is used to check if the operator on the left is null, if it returns the value on the right, otherwise, it returns the value on the left, to know more see What is the meaning of the "??" operator.
The TrueValue
quer property should be used differently, as I'm not exactly sure how you're using DataGridView
, I'd rather not use it, but you might see an example here .
I'm not sure exactly how much data you'll be working on, but doing this for
every time a cell is checked / unchecked, you probably will not perform well, I thought of another solution, you might need to better, but it already serves as a starting point.
Create a global variable private int numeroCelulasMarcadas = 0;
, this variable will serve as a counter of marked cells, in event RowsAdded
of DataGridView
, do so:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridViewCheckBoxCell chk =
(DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells["chk"];
if ((bool)(chk.Value ?? false) == true)
{
numeroCelulasMarcadas++;
}
}
Now in event CellValueChanged
put this:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// se a célula alterada for a célula de interesse
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("chk"))
{
DataGridViewCheckBoxCell chk =
(DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells["chk"];
if ((bool)chk.Value == true)
{
numeroCelulasMarcadas++;
}
else
{
numeroCelulasMarcadas--;
}
if (numeroCelulasMarcadas == 0)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
}
Remember that this last solution is a starting point, it may be necessary to adjust something to work correctly in the context of your application.