How to work with DataGridViewCheckBox using a separate chekBox that allows to enable and disable all fields of DataGridView?

1

I'm having some difficulties in working with DataGridViewCheckBox using a chekBox aside, almost everything is working in normality, the problem is when using the select all option, which is the chekBox that I added the part, for example: when I select two lines and then click on select all, it happens that it disables the two lines selected and enables the other lines that were not selected. p>

Here's the code:

private void chk_Selecionar_CheckedChanged(object sender, EventArgs e)
{    
 foreach (DataGridViewRow row in dgv_Entrada.Rows)
{
 DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
 chk.Value = !(chk.Value == null ? false : (bool)chk.Value); 
}

How do I, when I click select all it enables all the fields and if by chance you already have a field enabled it keeps enabled, and when you disable the select all option, it disables all fields?

    
asked by anonymous 20.09.2017 / 13:27

1 answer

1

I changed your code to take into account the value of your checkbox that plays the role of "select everything". It is to function properly now;

foreach (DataGridViewRow row in dgv_Entrada.Rows)
{
   DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
   chk.Value = (chk.Value == null ? false : chk_Selecionar.Checked); // O nome do seu checkbox selecionar tudo acessando a propriedade "Checked"
}
    
20.09.2017 / 14:13