Hi,

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.


Thank

Ruby

Dani AI

Generated

The error "Cast from type 'DBNull' to type 'Boolean' is not valid" means the data-binding engine or your code tried to treat a database NULL as a Boolean. and were headed in the right direction — you must explicitly guard against DBNull.Value (not just Nothing) before calling ToString or letting ASP.NET bind the value to a CheckBox.Checked property.

Best practice: use a Template column with an unbound CheckBox, then set its Checked/Visible state in ItemDataBound after checking for DBNull. This keeps the binding engine from trying to convert a NULL to Boolean and preserves column layout (hiding the whole cell can shift columns). Also consider fixing the data at the source (SELECT ISNULL(mn_checked, 0) or COALESCE(mn_checked, 0)) if you want every row to always provide a Boolean.

Example approach (VB.NET — call the checkbox ID you actually used):

' In the ItemTemplate: <asp:CheckBox ID="chkID" runat="server" />

Protected Sub MyGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
  If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
    Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
    If drv Is Nothing Then Return

    Dim val As Object = drv("mn_checked")
    Dim cb As CheckBox = TryCast(e.Item.FindControl("chkID"), CheckBox)
    If cb Is Nothing Then Return

    If val IsNot Nothing AndAlso Not Convert.IsDBNull(val) Then
      cb.Checked = Convert.ToBoolean(val)
      cb.Visible = True
    Else
      cb.Visible = False
    End If
  End If
End Sub

Quick troubleshooting: set a breakpoint in ItemDataBound and inspect the actual value/type of drv("mn_checked"). If it's DBNull you know why the cast failed. If you must keep declarative binding, coalesce NULLs in SQL or return a default false so the binder never sees DBNull.

Recommended Answers

All 2 Replies

I think this is what you are attempting to do.

Scenario:

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 -->
<ItemTemplate>					
     <asp:checkbox ID="chkID" Runat="server" visible='<%# CheckVal(Container.DataItem("ID")) %>' />
</ItemTemplate>

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

Hi,
try this.

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

thanks,
Kedar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.