Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a datagrid which is bound to a dataset which itself gets filled via xml.

Now because the Dataset get filled via Xml nodes which contain no data/null are excluded completely(correct me if i'm wrong). I am however needing to know whether the data is null so that i may react in a particular way on the front end(grid).

So the solution i am attempting is by using the Datagrid ItemDataBound event. Wherein i am trying to determine if the column exists and then if not react accordingly by adding a column with the data i desire.

<pre lang="xml">Protected Sub grdDirectivesCancelling_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdDirectivesCancelling.ItemDataBound


          Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)

        Try
            If DRV.Row.Table.Columns.Contains("MyCustomColumn") Then
                Response.Write("<br>data in column value")
            Else
                'add data column
                DRV.Row.Table.Columns.Add("MyCustomColumn")
                'Response.Write("<br>no column value for MyCustomColumn")
            End If
        Catch ex As Exception
            Response.Write(ex)
        End Try



    End Sub




I however receive the following error:

System.NullReferenceException: Object reference not set to an instance of an object. being thrown by
MIDL
If DRV.Row.Table.Columns.Contains(&quot;MyCustomColumn&quot;) Then

:-(

Thanks.
Posted

put ur code inside if condition

If e.Item.DataItem Then

VB
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)

        Try
            If DRV.Row.Table.Columns.Contains("MyCustomColumn") Then
                Response.Write("<br>data in column value")
            Else
                'add data column
                DRV.Row.Table.Columns.Add("MyCustomColumn")
                'Response.Write("<br>no column value for MyCustomColumn")
            End If
        Catch ex As Exception
            Response.Write(ex)
        End Try


End If

now u try
 
Share this answer
 
v2
Hi,

Thank you for your assistance,

I have added as you suggested although i now receive another exception.

System.InvalidCastException: Conversion from type 'DataRowView' to type 'Boolean' is not valid

Any ideas would be appreciated.

Thank you.
 
Share this answer
 
Hi,

you can use

IsDBNull
 
Share this answer
 
Hi,

I managed to get this solved.

VB
If e.Item.ItemType = ListItemType.Item Or _
             e.Item.ItemType = ListItemType.AlternatingItem Then

         ...

End If



Thank you so much for the help guys! Thank you for pointing me in the right direction!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900