How can I sum Columns on a datagrid?

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

I placed some labels on the footertemplate of each column that I want to
sum.

But I dont know hot so sum and show the total in the labels.
 
S

Scott Mitchell [MVP]

Luis said:
I placed some labels on the footertemplate of each column that I want to
sum.

But I dont know hot so sum and show the total in the labels.

Luis, check out this article:

An Extensive Examination of the DataGrid: Part 13
http://aspnet.4guysfromrolla.com/articles/020503-1.aspx

In fact, you can read the entire 16-part series starting at
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
Q

q

Do the summation for Item & AlternatingItem in your ItemDataBound event
handler.
Display the total in the FooterItem
 
A

Alvin Bruney [MVP]

actually, you can sum in the footer by applying the appropriate filter
against the dataset. this approach assumes that the dataset and the datagrid
are in synch (they usually are)
 
A

Aaron

Hello,

I am trying to compute the row count total and display in the footer of a
detail grid as described in the article, and am getting the following error:
"System.Web.HttpException: DataBinder.Eval: 'System.Data.Common.DbDataRecord'
does not contain a property with the name ViewCount."

Am I missing something? I didn't read anything in the article about adding a
custom property. Maybe I'm misinterpreting the error....

Any thoughts?

:
 
F

Frankie

Aaron,
If you look closely at the article "ViewCount" is declared as an
asp:boundcolumn. You have to use your own datacolumn name. This should be
the same name as the column you want totaled.

So if you had a query that went something like this: "SELECT ProductName,
ProductPrice FROM Products" you could do something like the following
assuming you have only two columns in your datagrid (bound automatically).

Private Sub FormatGrid(byval sender as object, byval e as
DataGridItemEventArgs) Handles MyGrid.ItemDataBound
Static Total as Decimal
Select Case e.Item.ItemType
Case = ListITemType.Alternating, ListItemType.Item
' Increment the total here - Remember this was declared as static so the
value will remain with each successive call
Total += Decimal.Parse(DataBinder.Eval(e.Item.DataItem, "ProductPrice"))
' OR another way
' Total += Decimal.Parse(CType(e.Item.DataItem,
System.Data.Common.DBDataRecord)("ProductPrice"))
Case = ListItemType.Footer
With e.Item
.Cells(0).HorizontalAlign = HorizontalAlign.Right
.Cells(0).Text = "Total:"
.Cells(1).Text = String.Format("{0:C}", Total)
End With
End Select
End Sub
 
F

Frankie

Aaron,
If you look closely at the article "ViewCount" is declared as an
asp:boundcolumn. You have to use your own datacolumn name. This should be
the same name as the column you want totaled.

So if you had a query that goes something like this: "SELECT ProductName,
ProductPrice FROM Products" you could do something like the following
assuming you have only two columns in your datagrid (bound automatically).

Private Sub FormatGrid(byval sender as object, byval e as
DataGridItemEventArgs) Handles MyGrid.ItemDataBound
Static Total as Decimal
Select Case e.Item.ItemType
Case = ListITemType.Alternating, ListItemType.Item
' Increment the total here - Remember this was declared as static so the
value will remain with each successive call
Total += Decimal.Parse(DataBinder.Eval(e.Item.DataItem, "ProductPrice"))
' OR another way
' Total += Decimal.Parse(CType(e.Item.DataItem,
System.Data.Common.DBDataRecord)("ProductPrice"))
Case = ListItemType.Footer
With e.Item
.Cells(0).HorizontalAlign = HorizontalAlign.Right
.Cells(0).Text = "Total:"
.Cells(1).Text = String.Format("{0:C}", Total)
End With
End Select
End Sub
 

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

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top