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

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.