Using the DataGrid's ItemCreated event on PostBack

Discussion in 'ASP .Net Datagrid Control' started by Stephen Miller, Mar 3, 2004.

  1. 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
    Stephen Miller, Mar 3, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learning SQL Server

    Re: getting column value in ItemCreated datagrid event

    Learning SQL Server, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    529
    Learning SQL Server
    Aug 12, 2003
  2. Lars Netzel
    Replies:
    1
    Views:
    1,547
    Greg Burns
    Sep 10, 2004
  3. DC
    Replies:
    3
    Views:
    1,353
    sbellouti
    Nov 9, 2006
  4. archana
    Replies:
    2
    Views:
    552
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Apr 10, 2007
  5. Maulik shah via .NET 247

    Datagrid ItemCreated event & Edit botton - vb.net

    Maulik shah via .NET 247, May 18, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    358
    Maulik shah via .NET 247
    May 18, 2005
Loading...

Share This Page