You can do this using the event CellValueChanged
that way:
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;
}
}
Follow some points of attention:
- I used a
if
before checking the values of DataGridViewCheckBoxCell
to prevent verification from occurring in all columns.
- As I said in the comments, it was necessary to amend
dataGridView1.CurrentRow
for row
, for otherwise their for
would not run all lines, but rather only the current line (referring to altered cell).
- The
??
serves to check if the operator left of it is null, if that is, returns the value on the right, otherwise returns the value on the left, to know more see What is the meaning of the operator "??".
- The estate
TrueValue
should be used differently, as I do not know exactly how you are using the DataGridView
, I preferred not to use it, but you can see an example here.
I don’t know exactly what volume of data you will be working on, but do this for
every time a cell is checked/unchecked, you probably won’t perform well, I thought of another solution, maybe you need to improve it, but already serves as a starting point.
Create a global variable private int numeroCelulasMarcadas = 0;
, this variable will serve as a marked cell counter, in the 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 at the 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 solution that I passed last is a starting point, it may be necessary to adjust something to work correctness in the context of your application.
I am without Visual Studio to test, but try to change
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.CurrentRow.Cells["chk"];
forDataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["chk"];
, if it works I’ll create an answer!– mateusalxd
@Matthew It hasn’t worked yet. The values
chk.Value
andchk.TrueValue
are withValue
=null
. I think I’m using the wrong method. But it was worth the help.– Jothaz