[VB.NET] DataGridViewCheckBoxColumn explanation


Description
     if you need to add checkbox in your datagridview (VB.NET ), using DataGridViewCheckBoxColumn is the solution. but the example over internet is really pain, i don't understand why they use directcast only to get value ?? it's really pain !!. this tutorial , i will explain a simple method to get some value from checkbox either TRUE or FALSE but only for UNBIND not BIND to Database, with UNBIND you can manually modified the checkbox.

How  to

1. create your gridview , add new column and change the type to DataGridViewCheckBoxColum, and bring colum name "Grant"

2. to get data or set the value of checkbox do this :
datagridview1.Rows(x).Cells("cell_name_checkbox").Value = True
and voila your checkbox now checked , if you want to change into uncheck just use False

3. if you have some method after change state you can use CurrentCellDirtyStateChanged
    here's the example :
    Private Sub DGVItemList_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGVItemList.CurrentCellDirtyStateChanged
        If DGVItemList.Rows(x).Cells("Grant").Value = True Then
            MsgBox("cell check")
        Else
            MsgBox("cell uncheck")
        End If
    End Sub

note : "x" mark is index, ROW and COLUMN index start from 0.

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih