transstates
asked on
Getting Value of Checkbox in a Datagridview
Good Morning All,
I'm using vb.net 2008. I'm loading a datagridview with records from a database. In the datagridview i have some checkboxes. Whenever I load the data, many of the checkbox columns are checked. When I try to retrieve the value to see if it's checked I always get "nothing". Here is what I'm doing:
This just loads the grid. I have more columns but I'm sure you get the point.
Dim dsData As DataSet = .GetData()
For Each dt In dsData.Tables
For Each dr In dt.Rows
datagrid.rows.add()
With datagrid.rows(intRow)
.Cells("field1").Value = dr.Item("field1").ToString 'Checkbox column
intRow += 1
End With
Next
Next
Then I'm doing this to get the value of the checkbox:
I'm doing this in a sub after the grid has been loaded.
Dim chkboxEval As DataGridViewCheckBoxCell = New DataGridViewCheckBoxCell(d gv.Current CellAddres s.X = 4)
chkboxEval = CType(dgv.Rows(intRow).Cel ls(4), DataGridViewCheckBoxCell)
If chkboxEval.Value = True Then
dgv.Rows(intRow).Cells("Fi eld1").Rea dOnly = True
dgv.Rows(intRow).Cells("Fi eld2").Rea dOnly = True
Else
dgv.Rows(intRow).Cells("Fi eld1").Rea dOnly = True
dgv.Rows(intRow).Cells("Fi eld2").Rea dOnly = True
End If
I've even tried, If chkboxE.Value = chkboxE.TrueValue ....
The value of chkboxEval.value (even when checked) = Nothing.
Any ideas what I'm doing wrong or not doing that I should be?
Thanks in advance!
Jim
I'm using vb.net 2008. I'm loading a datagridview with records from a database. In the datagridview i have some checkboxes. Whenever I load the data, many of the checkbox columns are checked. When I try to retrieve the value to see if it's checked I always get "nothing". Here is what I'm doing:
This just loads the grid. I have more columns but I'm sure you get the point.
Dim dsData As DataSet = .GetData()
For Each dt In dsData.Tables
For Each dr In dt.Rows
datagrid.rows.add()
With datagrid.rows(intRow)
.Cells("field1").Value = dr.Item("field1").ToString
intRow += 1
End With
Next
Next
Then I'm doing this to get the value of the checkbox:
I'm doing this in a sub after the grid has been loaded.
Dim chkboxEval As DataGridViewCheckBoxCell = New DataGridViewCheckBoxCell(d
chkboxEval = CType(dgv.Rows(intRow).Cel
If chkboxEval.Value = True Then
dgv.Rows(intRow).Cells("Fi
dgv.Rows(intRow).Cells("Fi
Else
dgv.Rows(intRow).Cells("Fi
dgv.Rows(intRow).Cells("Fi
End If
I've even tried, If chkboxE.Value = chkboxE.TrueValue ....
The value of chkboxEval.value (even when checked) = Nothing.
Any ideas what I'm doing wrong or not doing that I should be?
Thanks in advance!
Jim
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That .Value should work for the checkbox. What code do you have to make it readonly?
ASKER
Ok, this works but when the sub is done and I look at the grid, while the row is checked the code to make it read only is not working correctly. hmmm..
I see it now... I was runing code after the fact that was messing with the rights of the grid.
Thank you!