Capture selected value in grid checkbox

1

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

I placed the checkbox inside this Gride. I would like you to only record items that are checked out of the checkbox. How can I do this?

    
asked by anonymous 20.10.2015 / 03:32

3 answers

1

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)
              {
              }
           }
    
19.11.2015 / 22:05
3

You need to go through all lines in DataGridView and check that CheckBox is checked, 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
    }
}
    
20.10.2015 / 04:05
2

An example of how to get checked values in 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 here .

See how to manipulate the checkbox on the grid here .

    
20.10.2015 / 04:15