Capture selected value in grid checkbox

Asked

Viewed 3,646 times

1

inserir a descrição da imagem aqui

Hello, I have the form above where it shows all the products I have registered in my database and presents in a Gride.

I put inside this Gride the option to select (checkbox). I would like you to save only the items that are "checked" from the checkbox. How can I do that?

  • Dude, explain your idea better. Edit your question and tell us what you’ve tried, enter the code you already have. This way your question will be closed because it is not clear enough.

  • See if it helps @jbueno

  • Already better, you just want it? Save and the rest you’ve got ready?

  • Yes, I even have other grids here, but without the "check box"

4 answers

3

You need to go through all the lines on DataGridView and check whether the CheckBox is marked in this way

foreach(DataGridViewRow linha in dgView.Rows){ // passar por todas as linhas do dg
    var cell = linha.Cells["ColunaDoCheckBox"] as DataGridViewCheckBoxCell; //pegar a celula que tem o checkbox

    if((bool)cell.Value){ //verificar se está marcado
         //salvar registro
    }
}
  • I will test this solution. Thank you!

  • Buddy, the Row. Cells command says it doesn’t exist. Is this the one?

2

An example of how to pick up the values marked on checkbox:

List<int> codigos = new List<int>();

    if (bool.Parse(DataGridView.CurrentRow.Cells[2].FormattedValue.ToString()) == true)
    {
        foreach (DataGridViewRow check in DataGridView.Rows)
        {
            if ((bool)check.Cells[2].FormattedValue)
            {
                b = int.Parse(check.Cells[0].Value.ToString());                                               
            }
        }

        codigos.Add(b); 
    }
    else
        MessageBox.Show("Selecione um item");

Returns the codes of the selected items in checkbox.

I took from here.

See more information on how to manipulate grid checkbox here.

1


Despite the accepted answer, it may contain null and when accessing the value of it give a Nullreferenceexception, I would change your implementation to check the billed checkbox value for

DataGridViewCheckBoxCell cell;
            foreach (DataGridViewRow linha in dgView.Rows)
            {
              cell = linha.Cells["nome da coluna (ou o índice)"] as DataGridViewCheckBoxCell;// linha.Cells["nomeDaColuna"] ou linha.Cells[0]
              bool bChecked = (null != cell && null != cell.Value && true == (bool)cell.Value);
              if (bChecked)
              {
              }
           }
  • Dude, I had this null error I commented on. I put the code you sent me, solved it. Thank you.

  • 1

    Glad you could help. It’s always good to check if it’s null, since you can assume 3 values null, true and false.

  • Just one detail, it’s Nullreferenceexception, Nullpointerexception is in Java.

  • valeu @jbueno corrected this detail

0

//dataGridView1 coloca o nome do seu Objeto dataGrid
foreach (DataGridViewRow Linha in dataGridView1.Rows)//Percorre o dataGrid e joga valor em 
{
    if (bool.Parse(Linha.Cells[0].EditedFormattedValue.ToString()))//Pega os valores com checkbox clicados (TRUE)
    {
     MessageBox.Show("Resultado: " + Linha.Cells[2].Value);//Faca a funcao.
    }
}

Browser other questions tagged

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