ItemCreated 
Author Message
 ItemCreated

I found this code in a book which makes the text red for a column in a
datagrid if it is less than 300.  It works fine but the problem is when you
click on another page number.  Then I get this error:
System.NullReferenceException: Object reference not set to an instance of an
object.  (on the Dim line)  It appears that e.Item.DataItem is Nothing.  I
tried testing for Nothing but apparently do not have the syntax correct.
Anyone have an idea on how to fix this?  Thanks.

Public Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemCreated

    If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem) Then

            Dim iUnitsInStock As Integer =
Integer.Parse(DataBinder.Eval(e.Item.DataItem, "ID").ToString())

            If (iUnitsInStock <= 300) Then
                e.Item.Cells(0).ForeColor = System.Drawing.Color.Red
            End If

        End If

    End Sub



Tue, 13 Jul 2004 00:34:26 GMT  
 ItemCreated
try this, I think this is correct----  "if not(object is nothing) then"
Cade


Quote:
> I found this code in a book which makes the text red for a column in a
> datagrid if it is less than 300.  It works fine but the problem is when
you
> click on another page number.  Then I get this error:
> System.NullReferenceException: Object reference not set to an instance of
an
> object.  (on the Dim line)  It appears that e.Item.DataItem is Nothing.  I
> tried testing for Nothing but apparently do not have the syntax correct.
> Anyone have an idea on how to fix this?  Thanks.

> Public Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As
> System.Web.UI.WebControls.DataGridItemEventArgs) Handles
> DataGrid1.ItemCreated

>     If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
> ListItemType.AlternatingItem) Then

>             Dim iUnitsInStock As Integer =
> Integer.Parse(DataBinder.Eval(e.Item.DataItem, "ID").ToString())

>             If (iUnitsInStock <= 300) Then
>                 e.Item.Cells(0).ForeColor = System.Drawing.Color.Red
>             End If

>         End If

>     End Sub



Tue, 13 Jul 2004 04:23:08 GMT  
 ItemCreated
That worked.  Thanks.


Tue, 13 Jul 2004 05:13:50 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. ItemCreated Event For DataGrid

 

 
Powered by phpBB® Forum Software