How can I sum Columns on a datagrid?

Discussion in 'ASP .Net Datagrid Control' started by Luis Esteban Valencia, Oct 20, 2004.

  1. 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 ESTEBAN VALENCIA
    MICROSOFT DCE 2.
    MIEMBRO ACTIVO DE ALIANZADEV
    Luis Esteban Valencia, Oct 20, 2004
    #1
    1. Advertising

  2. Luis Esteban Valencia wrote:
    > 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

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Oct 20, 2004
    #2
    1. Advertising

  3. Luis Esteban Valencia

    Guest

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

    Luis Esteban Valencia wrote:

    > 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.
    >
    >
    , Oct 21, 2004
    #3
  4. 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)

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    <> wrote in message news:...
    > Do the summation for Item & AlternatingItem in your ItemDataBound event
    > handler.
    > Display the total in the FooterItem
    >
    > Luis Esteban Valencia wrote:
    >
    >> 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.
    >>
    >>

    >
    Alvin Bruney [MVP], Oct 21, 2004
    #4
  5. Luis Esteban Valencia

    Aaron Guest

    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?

    "Scott Mitchell [MVP]" wrote:

    <Snip>

    >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
    >
    > http://www.4GuysFromRolla.com
    >
    > * When you think ASP.NET, think 4GuysFromRolla.com!
    >
    Aaron, Dec 30, 2004
    #5
  6. Luis Esteban Valencia

    Frankie Guest

    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

    "Aaron" <> wrote in message
    news:...
    > 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?
    >
    > "Scott Mitchell [MVP]" wrote:
    >
    > <Snip>
    >
    >>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
    >>
    >> http://www.4GuysFromRolla.com
    >>
    >> * When you think ASP.NET, think 4GuysFromRolla.com!
    >>
    Frankie, Jan 1, 2005
    #6
  7. Luis Esteban Valencia

    Frankie Guest

    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
    "Aaron" <> wrote in message
    news:...
    > 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?
    >
    > "Scott Mitchell [MVP]" wrote:
    >
    > <Snip>
    >
    >>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
    >>
    >> http://www.4GuysFromRolla.com
    >>
    >> * When you think ASP.NET, think 4GuysFromRolla.com!
    >>
    Frankie, Jan 12, 2005
    #7
    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. Roger Moore
    Replies:
    0
    Views:
    743
    Roger Moore
    Jul 7, 2003
  2. mark4asp
    Replies:
    2
    Views:
    610
    Jasbird
    Nov 11, 2006
  3. rob thomson
    Replies:
    0
    Views:
    253
    rob thomson
    Sep 4, 2003
  4. ton
    Replies:
    2
    Views:
    200
    Alvin Bruney [MVP]
    Feb 11, 2004
  5. Replies:
    9
    Views:
    119
Loading...

Share This Page