Professional Web Applications Themes

How can I sum Columns on a datagrid? - ASP.NET Data Grid Control

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...

  1. #1

    Default How can I sum Columns on a datagrid?

    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 Guest

  2. #2

    Default Re: How can I sum Columns on a datagrid?

    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
    [url]http://aspnet.4guysfromrolla.com/articles/020503-1.aspx[/url]

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

    Happy Programming!

    --

    Scott Mitchell
    [email]mitchell4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]

    * When you think ASP.NET, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

  3. #3

    Default Re: How can I sum Columns on a datagrid?

    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.
    >
    >
    q@q.com Guest

  4. #4

    Default Re: How can I sum Columns on a datagrid?

    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 [url]http://mvp.support.microsoft.com/default.aspx][/url]
    Got tidbits? Get it here... [url]http://tinyurl.com/27cok[/url]
    <qq.com> wrote in message news:41775E18.4050603q.com...
    > 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] Guest

  5. #5

    Default Re: How can I sum Columns on a datagrid?

    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
    > [url]http://aspnet.4guysfromrolla.com/articles/020503-1.aspx[/url]
    >
    > In fact, you can read the entire 16-part series starting at
    > [url]http://aspnet.4guysfromrolla.com/articles/040502-1.aspx[/url]
    >
    > Happy Programming!
    >
    > --
    >
    > Scott Mitchell
    > [email]mitchell4guysfromrolla.com[/email]
    > [url]http://www.4GuysFromRolla.com[/url]
    >
    > * When you think ASP.NET, think 4GuysFromRolla.com!
    >
    Aaron Guest

  6. #6

    Default Re: How can I sum Columns on a datagrid?

    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.P(DataBinder.Eval(e.Item.DataItem, "ProductPrice"))
    ' OR another way
    ' Total += Decimal.P(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" <Aarondiscussions.microsoft.com> wrote in message
    news:881E14F9-E186-40A5-80B4-78D3298DBFB4microsoft.com...
    > 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
    >> [url]http://aspnet.4guysfromrolla.com/articles/020503-1.aspx[/url]
    >>
    >> In fact, you can read the entire 16-part series starting at
    >> [url]http://aspnet.4guysfromrolla.com/articles/040502-1.aspx[/url]
    >>
    >> Happy Programming!
    >>
    >> --
    >>
    >> Scott Mitchell
    >> [email]mitchell4guysfromrolla.com[/email]
    >> [url]http://www.4GuysFromRolla.com[/url]
    >>
    >> * When you think ASP.NET, think 4GuysFromRolla.com!
    >>

    Frankie Guest

  7. #7

    Default Re: How can I sum Columns on a datagrid?

    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.P(DataBinder.Eval(e.Item.DataItem, "ProductPrice"))
    ' OR another way
    ' Total += Decimal.P(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" <Aarondiscussions.microsoft.com> wrote in message
    news:881E14F9-E186-40A5-80B4-78D3298DBFB4microsoft.com...
    > 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
    >> [url]http://aspnet.4guysfromrolla.com/articles/020503-1.aspx[/url]
    >>
    >> In fact, you can read the entire 16-part series starting at
    >> [url]http://aspnet.4guysfromrolla.com/articles/040502-1.aspx[/url]
    >>
    >> Happy Programming!
    >>
    >> --
    >>
    >> Scott Mitchell
    >> [email]mitchell4guysfromrolla.com[/email]
    >> [url]http://www.4GuysFromRolla.com[/url]
    >>
    >> * When you think ASP.NET, think 4GuysFromRolla.com!
    >>

    Frankie Guest

  8. #8

    Default Re: How can I sum Columns on a datagrid?


    Hi
    I have a problem to summing up datagrid values. these values ar
    getting generated at runtime. these columns are template columns .unde
    one column header there are 4 values in 2 rows and 2 columns like

    A B
    X 10 5 ------
    20 10 ------

    Y 15 0 ------
    20 30 ---- (so on....)

    I need to get at the end of the datagrid.

    25 (10+15) 5 (5+0) -------
    40 (20+20) 40 (10+30) -------


    if you have an idea on this please do reply.
    Regards
    request

    --
    reques
    -----------------------------------------------------------------------
    request's Profile: [url]http://www.highdots.com/forums/member.php?userid=26[/url]
    View this thread: [url]http://www.highdots.com/forums/showthread.php?t=69720[/url]

    request Guest

Similar Threads

  1. Add columns to datagrid on the fly!
    By VBTECH in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: April 21st, 08:51 AM
  2. Replies: 0
    Last Post: April 5th, 03:58 AM
  3. DataGrid Columns
    By bspann in forum ASP.NET Data Grid Control
    Replies: 6
    Last Post: December 30th, 10:12 PM
  4. Datagrid with SQL keywords on their columns
    By Luis Esteban Valencia in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: October 11th, 05:56 PM
  5. Columns and Inherited Datagrid...Active Schema does not support columns
    By rob thomson in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: September 4th, 03:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139