Get the Boolean of a DataGridView

0

Good morning! I have my dataGrid that is filled with data from my database and I made an additional column of type checkbox so that certain marked orders are billed. But what happens to me is that I have to update an LBL according to the selected orders with their respective amounts and it only updates me when the selected cell loses focus and I need it to be when I select the value true or false directly. I tried the following code attached to the event dataGridView1_CellClick but it does not work, the value is always outdated. If I click on the value it is always the one that I had and not the new one.

        decimal totalSelected = 0;
        dataGridView1.Focus();

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;

            if (cell.Value != null)
            {
                if (cell.Value == cell.TrueValue)
                {
                    totalSelected += Convert.ToDecimal(row.Cells[3].Value);
                }
            }
        }
        lblTotalToPaid.Text = totalSelected.ToString("$0.00", CultureInfo.CurrentCulture);
    }
    
asked by Mariano Veloso de brito 21.02.2017 в 17:24
source

1 answer

1

What you have to do is, in the event CellClick execute a CommitEdit . In this way, the event% co_of% that already contains the correct value is triggered. It would be something like this:

private void dataGridView1_CellContentClick(object sender, 
DataGridViewCellEventArgs e)
{
     dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}



private void dataGridView1_CellValueChanged(object sender, 
DataGridViewCellEventArgs e)
{
     //Aqui tu código
}

In any case, the code you use does not know if it is very efficient, since every time you modify a checkbox you will go through all the rows of CellValueChanged . I would think of storing the DataGridView value in a local property and only adding / subtracting the value of the checkbox row.

    
answered by 21.02.2017 / 17:49
source