Ask a Question related to ASP.NET Data Grid Control, Design and Development.
-
Stephen Miller #1
Using the DataGrid's ItemCreated event on PostBack
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 Guest
-
Possible to tell what generated a postback event?
I'm writing a webpage with a datagrid that has pagenation (is that a word? LOL) and has some buttons in a column that I put there dynamically in an... -
getting column value in ItemCreated datagrid event
got it. use DataItem("columnname"( "Learning SQL Server" <no.mail.com> wrote in message news:ulsj5VNYDHA.4040@tk2msftngp13.phx.gbl...... -
how to fire a postback event
Hi, All I design a composite custom control, which has three dropdownlists in it. I find as long as one of the dropdownlist control's id is... -
DataGrid's UpdateCommand event handler and CancelCommand handler problem
I am having the same problem: the wrong event handler is being fired when column headings and page changes are clicked. I am using the datagrid... -
why datagrid's itemcommand event fired when I press ENTER key in a TextBox
Hi, When you press the ENTER key the default button pressed and since the only button is the column template one a post back to the server...



Reply With Quote

