DataGridViewCheckBoxColumn - change the state of a checkbox

G

Guest

I have an unbound DataGridView with a DataGridViewCheckBoxColumn as column
2(configured in the designer). How would I programatically set the checkbox
state in, say, row 3 column 2?

I need to do this in two contexts. One is to make Checked the default in
this column when rows are created (the RowsAdded event might help me here,
although I suspect not for the first row). The other context is to
check/uncheck a checkbox in an existing row.

As you might guess I am new to this. Any help greatly appreciated.

Thanks

Bill.
 
K

Kevin Yu [MSFT]

Hi Bill,

To set the checkbox value in DataGridView programatically, you can set the
value of that cell directly. For example, you need to set the checkbox
Checked in row 3 col 2, you can use

this.dataGridView1.Rows[3].Cells[2].Value = true;

As far as I know, we cannot set the default value for the column in the
DataGridView if it is not unbound. Have to set the value for that column in
each row added with a line of code like above.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

You're welcome.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

i think that's only for web form... how bout how to set the checkbox in a
winform? i cant seem to set the state to be a checked checkbox.. pls help thnx
 
G

Gene Valgene

i'm having teh same issue :( anyone know what might be happening? i'm
setting the value of the cell as with the webform example above, but in
winform, doesn't seem to work. i can set the value, and retrieve it, but the
GUI doesn't reflect the changes. i believe my TrueValue and FalseValue are
set up correctly as well...anyone know what i may be missing?

Thanks!

Ejay said:
i think that's only for web form... how bout how to set the checkbox in a
winform? i cant seem to set the state to be a checked checkbox.. pls help thnx

Kevin Yu said:
Hi Bill,

To set the checkbox value in DataGridView programatically, you can set the
value of that cell directly. For example, you need to set the checkbox
Checked in row 3 col 2, you can use

this.dataGridView1.Rows[3].Cells[2].Value = true;

As far as I know, we cannot set the default value for the column in the
DataGridView if it is not unbound. Have to set the value for that column in
each row added with a line of code like above.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top