Link to home
Start Free TrialLog in
Avatar of transstates
transstatesFlag for United States of America

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(dgv.CurrentCellAddress.X = 4)
chkboxEval = CType(dgv.Rows(intRow).Cells(4), DataGridViewCheckBoxCell)
 
If chkboxEval.Value = True Then
     dgv.Rows(intRow).Cells("Field1").ReadOnly = True
     dgv.Rows(intRow).Cells("Field2").ReadOnly = True
Else
     dgv.Rows(intRow).Cells("Field1").ReadOnly = True
     dgv.Rows(intRow).Cells("Field2").ReadOnly = 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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of transstates

ASKER

It has the value "true".  I think this might be it... brb

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!
That .Value should work for the checkbox. What code do you have to make it readonly?