How to work with Datagridviewcheckbox using a chekBox apart that allows enabling and disabling all Datagridview fields?

Asked

Viewed 26 times

1

I’m having a hard time working with Datagridviewcheckbox using a chekBox apart, almost everything is working in normality, the problem is when using the option to select everything, which is the chekBox that I added the part, for example: when I select two lines and then click on select everything, it turns out that it disables the two lines selected and enables the other lines that were not selected.

Here is 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, so that when I click select everything it enables all fields and if by chance you already have a field enabled it keeps enabled, and when disable the select all option, it disables all fields?

  • 'chk. Value = true;' will leave all fields enabled. This code is used to disable as well?

  • Yeah, that’s right, that’s right

1 answer

1


I changed your code to take into account the value of your checkbox that plays the role of "select all". It is to work 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"
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.