S
Stephen Miller
It's easy to create a column total on a BoundColumn and add to the
column footer, when you know with certainty the column index to be
sub-totalled. For example:
Private Sub myDataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
myDataGrid.ItemCreated
Static dTotal As Decimal = 0
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
dTotal += CType(e.Item.Cells(1).Text, Decimal)
ElseIf e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(1).Text = dTotal
End If
End Sub
However, I need to achieve the same result using the column name (I
can't be certain that the developer won't move columns around). I can
easily read data using this method, but I can't find a way to write to
a cell item.
Private Sub myDataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
myDataGrid.ItemCreated
Static dTotal As Decimal = 0
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
dTotal += CType(e.Item.DataItem("myValue"), Decimal)
ElseIf e.Item.ItemType = ListItemType.Footer Then
' PROBLEM: Ideally I would bind to the footer like this:
' e.Item.DataItem("myValue") = dTotal.toString
' or:
' e.Item.Cells(CType(e.Item.DataItem("myValue"),
DataGridItem).ItemIndex()).Text = dTotal
' what would be the correct syntax?
End If
End Sub
I know I can do this using a TemplateColumn (rather than a
BoundColumn) and adding Label or Textbox to the FooterTemplate and
referencing in code like:
CType(e.Item.FindControl("SubTotal"), Label).Text = dTotal.toString
However, I'm trying to avoid this and achieve a similar result with a
BoundColumn. Can this be done?
Thanks,
Stephen
column footer, when you know with certainty the column index to be
sub-totalled. For example:
Private Sub myDataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
myDataGrid.ItemCreated
Static dTotal As Decimal = 0
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
dTotal += CType(e.Item.Cells(1).Text, Decimal)
ElseIf e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(1).Text = dTotal
End If
End Sub
However, I need to achieve the same result using the column name (I
can't be certain that the developer won't move columns around). I can
easily read data using this method, but I can't find a way to write to
a cell item.
Private Sub myDataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
myDataGrid.ItemCreated
Static dTotal As Decimal = 0
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
dTotal += CType(e.Item.DataItem("myValue"), Decimal)
ElseIf e.Item.ItemType = ListItemType.Footer Then
' PROBLEM: Ideally I would bind to the footer like this:
' e.Item.DataItem("myValue") = dTotal.toString
' or:
' e.Item.Cells(CType(e.Item.DataItem("myValue"),
DataGridItem).ItemIndex()).Text = dTotal
' what would be the correct syntax?
End If
End Sub
I know I can do this using a TemplateColumn (rather than a
BoundColumn) and adding Label or Textbox to the FooterTemplate and
referencing in code like:
CType(e.Item.FindControl("SubTotal"), Label).Text = dTotal.toString
However, I'm trying to avoid this and achieve a similar result with a
BoundColumn. Can this be done?
Thanks,
Stephen