2

DataGridViewCheckBoxCell is not getting checked. I have inserted the first column like this

DataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn { Name = "Print" });

This is not working

DataGridView1.Rows[0].Cells[0].Value = true;

Neither this is working

DataGridView1.Rows[0].Cells[0].Value = cell.TrueValue;

What could be the reason it is not getting checked?

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70

5 Answers5

3

I was having issues with this myself. I was using the following code, and I couldnt figure out why it wasn't working right all of the time.

(Note that my code is in VB, but I think the same concept would apply to other .NET languages)

Dim check As DataGridViewCheckBoxCell = DataGridView1.Rows(i).Cells("columnNameHere")
If check.Value = check.TrueValue Then
    'do stuff
End If

I then realized that it was because the underlying value of the cell isnt changed until it loses focus. I think DGVs always behave like this, but it can be easy to forget.

My solution was to simply add a little bit of code to the data grid views on click event handler. When the user clicks the checkbox, all it does is shift the focus elsewhere. I shifted the focus to a label so that it doesnt have any unintended consequences. (Ex: if you shift it to a button the user might be surprised when they press enter and a random button activates.) Depending on what else is in your DGV, you may want to check the column index of the event so that you are only doing this for the checkbox columns. All of the other columns in my DGV are read only anyways, so it didnt matter in my case.

Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
     Label1.Focus()
End Sub

If the code in question was contained in a button, the focus would already be shifted away from the DGV to the button, so this wouldn't be an issue. In my case, the code was activated by a timer - meaning that the DGV cell in question wouldn't necissarily lose focus before the timer fired.

Allen
  • 927
  • 8
  • 19
1

your first line of code actually worked for me:

DataGridView1.Rows[0].Cells[0].Value = true;

checked the first line. the second line isn't working because there is no meaning to cell.TrueValue, and a cell's property TrueValue is not a const of a checked check box

let me just add that the way you address your DataGridView's properties is not very safe and can cause exceptions

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
1

I don't know why it is not working for you, but you can try following method instead.

dataGridView1.Rows[0].SetValues(true);

This will only check the first item. When you want to set values for more cells, just use more parameters.

var values = new bool[] { true, false, true };
dataGridView1.Rows[0].SetValues(values);

This will check the first and the third cell, but the second cell will remain unchecked.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
1

Is your datagridview Edit Mode property is set to Edit Programmatically ?

I was having the same issue, So I changed my datagridview Edit Mode property to Edit on Enter. Now it's working fine for me.

Please set your datagridview Edit Mode to Edit on Enter.

If you want to make other (Non Checkbox) columns read only use DataDridView1.Column("Column Name Here Or Index").Readonly = True to made them read only.

Sorry For Late, But other searchers can get some helps from this answer.

Muhammad Saqib
  • 2,185
  • 3
  • 35
  • 48
1

If you just want to add new checkbox column and want to check`uncheckcheckboxes in column you need to addTrueValueandFalseValuefor yourCheckBoxColumn`. Go to the Edit Columns dialog and set TrueValue and FalseValue attributes for your column. Thanks.

maxshuty
  • 9,708
  • 13
  • 64
  • 77