Accessing CheckBox on row in Datagridview
-
Why on earth it's so difficult to find a sample on the web for this topic? Nobody has ever tried putting a checkbox on a datagridview on a winform? Or my search is wrong? This thread's subject has been my search key. Gettin nothin. What's wrong with the below code?
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[0].Cells[0]; if (chk.Selected) { MessageBox.Show("Selected!"); }
It's always checking the "cells" selected state. I want to know if the check box has been selected(Checked!) or not. Yes, I added column 0 to be a DataGridViewCheckBoxCell. Anybody can help?:beer:
-
Why on earth it's so difficult to find a sample on the web for this topic? Nobody has ever tried putting a checkbox on a datagridview on a winform? Or my search is wrong? This thread's subject has been my search key. Gettin nothin. What's wrong with the below code?
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[0].Cells[0]; if (chk.Selected) { MessageBox.Show("Selected!"); }
It's always checking the "cells" selected state. I want to know if the check box has been selected(Checked!) or not. Yes, I added column 0 to be a DataGridViewCheckBoxCell. Anybody can help?:beer:
-
The Value property would give the checked state of the cell. :) BTW, instead of casting, you can use
as
keyword along with the null check. -
How do I access it's value? The below attempt is successfuly throwing a cast exception.
if (CheckState.Checked == (CheckState)dataGridView1.Rows[0].Cells[0].Value) { MessageBox.Show("ok"); }
:beer:
so look at
dataGridView1.Rows[0].Cells[0].Value.ToString()
to discover what might be wrong. everything responds well to ToString(), except null of course. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
How do I access it's value? The below attempt is successfuly throwing a cast exception.
if (CheckState.Checked == (CheckState)dataGridView1.Rows[0].Cells[0].Value) { MessageBox.Show("ok"); }
:beer:
-
so look at
dataGridView1.Rows[0].Cells[0].Value.ToString()
to discover what might be wrong. everything responds well to ToString(), except null of course. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
Something's troubling. It requires me to "check" the checkbox at least once before checking it's VALUE to stop it from whining. If I call
dataGridView1.Rows[0].Cells[0].Value.ToString()
straight, without touching the grid, it's throwing NULL object exception.:beer:
-
Something's troubling. It requires me to "check" the checkbox at least once before checking it's VALUE to stop it from whining. If I call
dataGridView1.Rows[0].Cells[0].Value.ToString()
straight, without touching the grid, it's throwing NULL object exception.:beer:
Smith# wrote:
It requires me to "check" the checkbox at least once before checking it's VALUE
Not quite. There must be many ways to make Value hold an appropriate value, or to overcome a possible null. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Essentially
.Value
can have 3 values in the case of aCheckBox
:true
,false
andnull
. Whenever you deal withDataGridView
cell values, you need to check fornull
. Whether it's aCheckBox
cell or not, accessing anull
is bad.CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
Essentially
.Value
can have 3 values in the case of aCheckBox
:true
,false
andnull
. Whenever you deal withDataGridView
cell values, you need to check fornull
. Whether it's aCheckBox
cell or not, accessing anull
is bad.CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
Hi Walt Fair, thanks for your reply. Well,I'm still not convinced with the replies. The grid has been drawn on the screen, the rows have been created, the cells are displayed beautifully with checkboxes. Then why the "Value" has to be NULL? and why it's not NULL after I check the cell once?
:beer: