Getting Value of Checkbox in a Datagridview

Posted on 2012-08-30
Last Modified: 2012-08-30
Good Morning All,

I'm using 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
           With datagrid.rows(intRow)
                 .Cells("field1").Value = dr.Item("field1").ToString 'Checkbox column
                 intRow += 1
           End With

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
     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!

Question by:transstates
    LVL 83

    Accepted Solution

    Have you tried



    What does it return?

    Author Comment

    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!
    LVL 83

    Expert Comment

    That .Value should work for the checkbox. What code do you have to make it readonly?

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now