'Why Datagridview UI does not get visually Check on Checkbox Colum after set the value from DB

What happen?

I have datagridview called dgv, with 1 unbound Checkboxcolumn the column name is 'Checklist' and 3 other from DB, Flag(bool), numbid, impnumb, and event Cell Content Click, if I Check the checkbox, code will get the value of Checklist, which is 'true' and store it to Database column name 'Flag', and its still fine. (I have to set column Flag.visible = true, later will false,try to learn reason for this conundrum)

Problem 1

private void dgv(object sender, DataGridViewCellEventArgs e)
        {
            
            _numid= dgv.Rows[e.RowIndex].Cells["numbid"].Value.ToString();
            _impnumb= dgv.Rows[e.RowIndex].Cells["impnumb"].Value.ToString();
            ch1 = (DataGridViewCheckBoxCell)dgv.Rows[dgv.CurrentRow.Index].Cells[0];
            //cells [0] is a Checkbox, since it start with null on DB, so I check
            if (ch1.Value == null) // like this and set it to false/0
                ch1.Value = false;
            switch (ch1.Value.ToString())
            {
                case "True":
                    ch1.Value = false;                    
                    FlagSearchReg(_numid, _imnumb, false);// this will update the data into false if checkbox remain uncheck
                     break;
                case "False":
                    ch1.Value = true;
                    FlagSearchReg(_numid, _imnumb, true);// this will update the data into false if checkbox get checked
                    break;

            }
dgv.Refresh();

in datagridview now I Have 4 Checklist (unbound), flag, numberid, impnumb, after that the Flag column still uncheck meanwhile unbound 'ch1' get check visually, on DB Flag already true values, what should I do to get both column on datagridview visually both get check?

problem 2 on the Form Load I write code

 private void UC_load(object sender, EventArgs e){
_mod = GetData(); // this is selecting 3 data from DB
dgv.DataSource = _mod;

foreach(DataGridViewRow row in dgv.Rows)
{
                ch1 = (DataGridViewCheckBoxCell)dgv.Rows[dgv.CurrentRow.Index].Cells["Checklist"];
                ch1.Value = row.Cells["Flag"].Value; 
// set the value of Checklist from DB of flag data that we save before
  }
dgv.Refresh();
}

How?

Problem 1

visually only column Flag get check, if I debug this, the ch1.value is programmatically correct on every row. but visually only Column Flag being Checked?

Problem 2 (happen when first time load) how can I call and set both of them visually checked because, I try to set the value of Checklist (unbound) from DB column flag?



Solution 1:[1]

Well… it is unclear (at least to me) what you are trying to do in that code. It is looping through the rows in the grid and setting some variable called ch1. It is unclear where ch1 is used. We can assume it is a DataGridViewCheckBoxCell, but what is it used for? You may want to [edit] your question to make it clear what you are wanting to do when the form is loaded as it is not very clear.

Also, I assume the first code snippet is the grids CellContentClick event. If it is, then you may not get the correct results from the cells value. Typically, this event will fire before the cells value actually changes, and it also appears to keep the same initial value no matter if the cell is checked or not. In my small tests, to get the correct value, you need to “commit” the change to the grid BEFORE you grab its value.

In addition, it is unnecessary to cast the cell to DataGridViewCheckBoxCell to get its value, we can simply look at the cells value. Something like…

private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e) {
  dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
  Debug.WriteLine("Row " + e.RowIndex + " checked is -> " + dgv.Rows[e.RowIndex].Cells[0].Value.ToString());
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JohnG