Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Using the DataGrid's ItemCreated event on PostBack

Reply
Thread Tools

Using the DataGrid's ItemCreated event on PostBack

 
 
Stephen Miller
Guest
Posts: n/a
 
      03-03-2004
I have a DataGrid with a series of transactions and wish to highlight
negative value items in red. This is simple on the initial DataBind,
by using the ItemDataBound event to format the ListItemType based on a
DataItem's value. Ie:

Private Sub myGrid_ItemDataBound(ByVal sender As Object, _
ByVal e As DataGridItemEventArgs) Handles myGrid.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
If CType(e.Item.DataItem("Balance"), Decimal) < 0 Then
e.Item.ForeColor = System.Drawing.Color.Red
End If
End If
End Sub

On the initial page load this code would work equally well in the
DataGrid's ItemCreated event. Ie:

Private Sub myGrid_ItemCreated (ByVal sender As Object, _
ByVal e As DataGridItemEventArgs) Handles myGrid.ItemCreated
If e.Item.ItemType = ListItemType.Item Then
If CType(e.Item.DataItem("Balance"), Decimal) < 0 Then
e.Item.ForeColor = System.Drawing.Color.Red
End If
End If
End Sub

However, on PostBack, the DataGrid is rebuilt from ViewState and only
the ItemCreated event is raised. My problem is that when the DataGrid
is rebuilt the original DataItem's appear to be unavailable. The
myGrid_ItemCreated method above fails on PostBack at the line:

If CType(e.Item.DataItem("Balance"), Decimal) < 0 Then
...

Placing a watch on the value e.Item.DataItem("Balance") reveals that
is has no value.

I had though I could refer to the actual data in the cell, where
(assuming the 'Balance' is in the first column):

If CType(e.Item.Cells(0).Text, Decimal) < 0 Then
...

However this also has no value (presumably because the Item hasn't
actually been created yet).

Similarly, using a TemplateColumn and placing the 'Balance' in a Label
and then usinging FindControl to retrieve the value also fails. Ie:

Dim lblBalance As Label = e.Item.FindControl("lblBalance")
If CType(lblBalance.Text, Decimal) < 0 Then
...


So the problem is, how do I parse a variable or condition in a
DataGrid that can be used in the ItemCreated event on PostBack to
provide custom formatting to the grid?

Thanks,

Stephen
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why Repeater.ItemCreated is fired before Page_Load on postback Philippe Camus ASP .Net 2 03-30-2006 12:57 PM
Datagrid ItemCreated happens Before Postback??? Lars Netzel ASP .Net 1 09-10-2004 12:14 PM
Accessing DataGridItem in ItemCreated Event. =?Utf-8?B?U291cmkgQ2hhbGxh?= ASP .Net 4 08-28-2004 03:25 PM
DataList's ItemCreated event Hardy Wang ASP .Net 3 10-27-2003 09:39 PM
Re: getting column value in ItemCreated datagrid event Learning SQL Server ASP .Net 0 08-12-2003 01:38 PM



Advertisments