2

I'm trying to check cells from a DataGridViewCheckBoxColumn. I want to do it programmatically.

Here is my DatagridView:

DataGridViewCheckBoxColumn dataChecked = new DataGridViewCheckBoxColumn(false);

this.dataGrid.Columns.Add(dataChecked);

My DataGrid is using a DataSource from sql but my CheckBoxColumn doesn't. That column is just a way to know later when I have to add or remove some data to/from a container.

Container 
    DATA 1 Check
    DATA 2 Uncheck

Container 
    DATA 1 Uncheck (remove)
    DATA 2 Check (add)

I tried multiple things but nothing worked:

datagrid.Rows[1].Cells[5].Value = true;
datagrid.Rows[1].Cells[5].TrueValue = true;
DataGridViewCell chk = (DataGridViewCell)datagrid.Rows[1].Cells[5];
chk.Value = true;

I can tell if the cell is true or not but the CheckBox is not checked...

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
Ed.Q
  • 23
  • 3
  • Possible duplicate of [Progmatically Checking "CheckBoxCell" in DataGridView](http://stackoverflow.com/questions/35709822/progmatically-checking-checkboxcell-in-datagridview) – OhBeWise Mar 18 '16 at 17:39
  • I'm sorry this didn't occur to me before, but where are you setting these cell values to `true`? Because what you've tried should work, unless you are doing this is in the `Form` constructor. – OhBeWise Jan 13 '17 at 21:28

2 Answers2

0

Try chk.Value=1 for true or 0 for false.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

you can try follow code after adding value to datagridview checkbnox:

YourDataGridview.EndEdit();

With this sample:

foreach (DataGridViewRow r in MyDataGridView.Rows)
            {
                r.Cells["colSelection"].Value = true;
                MyDataGridView.EndEdit();
            }
AliNajafZadeh
  • 1,216
  • 2
  • 13
  • 22