Summing a column and displaying in the footer

Discussion in 'ASP .Net Datagrid Control' started by William Gower, May 17, 2004.

  1. I would like to sum a column in my dataset and have it displayed at the
    bottom of the datagrid in the footer. How do I do that?
    William Gower, May 17, 2004
    #1
    1. Advertising

  2. Assuming your datagrid is bound to a dataset, you can do this:

    if(e.Item.ItemType == ListItemType.Footer)
    {
    //sum every column
    for(int i = 0; i < e.Items.Count - 1; i++)
    e.Item.Cells.Text = ds.Tables[0].Compute("Sum([" +
    e.Item.Cells.HeaderText + "])",String.Empty);

    roughly

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "William Gower" <> wrote in message
    news:...
    >I would like to sum a column in my dataset and have it displayed at the
    > bottom of the datagrid in the footer. How do I do that?
    >
    >
    Alvin Bruney [MVP], May 17, 2004
    #2
    1. Advertising

  3. William Gower

    Prodip Saha Guest

    Alvin,
    What will be the result if Sum encounters a NULL value? I am assuming IsNull
    function is not used in the SQL that populates the dataset.

    Thanks,
    Prodip

    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:...
    > Assuming your datagrid is bound to a dataset, you can do this:
    >
    > if(e.Item.ItemType == ListItemType.Footer)
    > {
    > //sum every column
    > for(int i = 0; i < e.Items.Count - 1; i++)
    > e.Item.Cells.Text = ds.Tables[0].Compute("Sum([" +
    > e.Item.Cells.HeaderText + "])",String.Empty);
    >
    > roughly
    >
    > --
    > Regards,
    > Alvin Bruney
    > [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    > Got tidbits? Get it here... http://tinyurl.com/27cok
    > "William Gower" <> wrote in message
    > news:...
    > >I would like to sum a column in my dataset and have it displayed at the
    > > bottom of the datagrid in the footer. How do I do that?
    > >
    > >

    >
    >
    Prodip Saha, May 18, 2004
    #3
  4. i'm not entirely sure but I believe it's gonna blow up like a weapon of mass
    destruction. guard the code with a try catch block.

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Prodip Saha" <> wrote in message
    news:...
    > Alvin,
    > What will be the result if Sum encounters a NULL value? I am assuming
    > IsNull
    > function is not used in the SQL that populates the dataset.
    >
    > Thanks,
    > Prodip
    >
    > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > news:...
    >> Assuming your datagrid is bound to a dataset, you can do this:
    >>
    >> if(e.Item.ItemType == ListItemType.Footer)
    >> {
    >> //sum every column
    >> for(int i = 0; i < e.Items.Count - 1; i++)
    >> e.Item.Cells.Text = ds.Tables[0].Compute("Sum([" +
    >> e.Item.Cells.HeaderText + "])",String.Empty);
    >>
    >> roughly
    >>
    >> --
    >> Regards,
    >> Alvin Bruney
    >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    >> Got tidbits? Get it here... http://tinyurl.com/27cok
    >> "William Gower" <> wrote in message
    >> news:...
    >> >I would like to sum a column in my dataset and have it displayed at the
    >> > bottom of the datagrid in the footer. How do I do that?
    >> >
    >> >

    >>
    >>

    >
    >
    Alvin Bruney [MVP], May 18, 2004
    #4
  5. William Gower

    Prodip Saha Guest

    Thanks Alvin. I wish I could use IsNull function along with the Sum but it
    is not allowed on Aggregate functions like Sum.

    Prodip

    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:%...
    > i'm not entirely sure but I believe it's gonna blow up like a weapon of

    mass
    > destruction. guard the code with a try catch block.
    >
    > --
    > Regards,
    > Alvin Bruney
    > [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    > Got tidbits? Get it here... http://tinyurl.com/27cok
    > "Prodip Saha" <> wrote in message
    > news:...
    > > Alvin,
    > > What will be the result if Sum encounters a NULL value? I am assuming
    > > IsNull
    > > function is not used in the SQL that populates the dataset.
    > >
    > > Thanks,
    > > Prodip
    > >
    > > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > > news:...
    > >> Assuming your datagrid is bound to a dataset, you can do this:
    > >>
    > >> if(e.Item.ItemType == ListItemType.Footer)
    > >> {
    > >> //sum every column
    > >> for(int i = 0; i < e.Items.Count - 1; i++)
    > >> e.Item.Cells.Text = ds.Tables[0].Compute("Sum([" +
    > >> e.Item.Cells.HeaderText + "])",String.Empty);
    > >>
    > >> roughly
    > >>
    > >> --
    > >> Regards,
    > >> Alvin Bruney
    > >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    > >> Got tidbits? Get it here... http://tinyurl.com/27cok
    > >> "William Gower" <> wrote in message
    > >> news:...
    > >> >I would like to sum a column in my dataset and have it displayed at

    the
    > >> > bottom of the datagrid in the footer. How do I do that?
    > >> >
    > >> >
    > >>
    > >>

    > >
    > >

    >
    >
    Prodip Saha, May 19, 2004
    #5
  6. You can,
    use ISNULL(expression, replacementvalue)
    as part of the expression and it will make the appropriate replacement. you
    can see the msdn docs for a working example

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Prodip Saha" <> wrote in message
    news:...
    > Thanks Alvin. I wish I could use IsNull function along with the Sum but it
    > is not allowed on Aggregate functions like Sum.
    >
    > Prodip
    >
    > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > news:%...
    >> i'm not entirely sure but I believe it's gonna blow up like a weapon of

    > mass
    >> destruction. guard the code with a try catch block.
    >>
    >> --
    >> Regards,
    >> Alvin Bruney
    >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    >> Got tidbits? Get it here... http://tinyurl.com/27cok
    >> "Prodip Saha" <> wrote in message
    >> news:...
    >> > Alvin,
    >> > What will be the result if Sum encounters a NULL value? I am assuming
    >> > IsNull
    >> > function is not used in the SQL that populates the dataset.
    >> >
    >> > Thanks,
    >> > Prodip
    >> >
    >> > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    >> > news:...
    >> >> Assuming your datagrid is bound to a dataset, you can do this:
    >> >>
    >> >> if(e.Item.ItemType == ListItemType.Footer)
    >> >> {
    >> >> //sum every column
    >> >> for(int i = 0; i < e.Items.Count - 1; i++)
    >> >> e.Item.Cells.Text = ds.Tables[0].Compute("Sum([" +
    >> >> e.Item.Cells.HeaderText + "])",String.Empty);
    >> >>
    >> >> roughly
    >> >>
    >> >> --
    >> >> Regards,
    >> >> Alvin Bruney
    >> >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    >> >> Got tidbits? Get it here... http://tinyurl.com/27cok
    >> >> "William Gower" <> wrote in message
    >> >> news:...
    >> >> >I would like to sum a column in my dataset and have it displayed at

    > the
    >> >> > bottom of the datagrid in the footer. How do I do that?
    >> >> >
    >> >> >
    >> >>
    >> >>
    >> >
    >> >

    >>
    >>

    >
    >
    Alvin Bruney [MVP], May 19, 2004
    #6
    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. Summing a list

    , Sep 22, 2005, in forum: XML
    Replies:
    2
    Views:
    512
  2. Alf P. Steinbach
    Replies:
    2
    Views:
    531
    Chris Theis
    Feb 5, 2004
  3. Henrik
    Replies:
    1
    Views:
    423
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
  4. flebber

    Using ternary and summing array

    flebber, Oct 31, 2011, in forum: C Programming
    Replies:
    11
    Views:
    501
    David Thompson
    Nov 21, 2011
  5. Chris Kaynor
    Replies:
    0
    Views:
    117
    Chris Kaynor
    Oct 3, 2013
Loading...

Share This Page