Adding a SubTotal to a named BoundColumn

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
 
A

Alvin Bruney [MVP]

One approach is to use the expression property of the underlying datasource
to total. This way, you can target a column name instead of an index.
Realize that the datagrid and it's underlying datasource must always be
in-sync. Normally this is not a problem.

total = datagrid.Tables[0].Compute("Sum([" + myColName + "])",String.Empty);
The use of braces are optional, it is required only if myColName contains
spaces. If myColName is not a valid columnname, or does not appear in the
dataset, an exception is thrown.
 
S

Stephen Miller

Alvin,

Thanks for your reply, but I think you misunderstand my problem. I
could use the compute method or a static variable to calculate the
total of a named column in a BoundColumn. My problem is that I don't
know how to write that total to the footer of the BoundColumn using
the column's name

ie

myColName.text = Total

Thanks,

Stephen

Alvin Bruney said:
One approach is to use the expression property of the underlying datasource
to total. This way, you can target a column name instead of an index.
Realize that the datagrid and it's underlying datasource must always be
in-sync. Normally this is not a problem.

total = datagrid.Tables[0].Compute("Sum([" + myColName + "])",String.Empty);
The use of braces are optional, it is required only if myColName contains
spaces. If myColName is not a valid columnname, or does not appear in the
dataset, an exception is thrown.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Stephen Miller said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top