I new to asp .net, and I have problem with checkbox bound in datagrid,
I want to detect when the field value is null the check box will hide / invisible.

I have try to code in onItemDataBound like this:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.EditItem Or e.Item.ItemType = ListItemType.SelectedItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
e.Item.Cells(2).Visible = (drv("mn_checked").ToString <> "")
end if


But still got error:
Cast from type 'DBNull' to type 'Boolean' is not valid.



I think this is what you are attempting to do.


DataGrid is bound with data, if a certain value in the row is null then do not display the checkbox.

Code (code-in-front):

<!-- Declaration of checkbox within DataGrid -->
     <asp:checkbox ID="chkID" Runat="server" visible='<%# CheckVal(Container.DataItem("ID")) %>' />

Code: (code-behind):

Public Function CheckVal(ByVal id As Object) As String
     Dim displayCheck As Boolean = True

     If id Is Nothing Then
            displayCheck = False
     End If

     Return displayCheck 
End Function

try this.

if(drv("mn_checked") != null && drv("mn_checked").ToString <> "")
e.Item.Cells(2).Visible = true;
e.Item.Cells(2).Visible = false;


This article has been dead for over six months. Start a new discussion instead.