Summing a column and displaying in the footer

W

William Gower

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?
 
A

Alvin Bruney [MVP]

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
 
P

Prodip Saha

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 said:
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 said:
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?
 
A

Alvin Bruney [MVP]

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 said:
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 said:
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 said:
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?

 
P

Prodip Saha

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 said:
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 said:
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 said:
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
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?


 
A

Alvin Bruney [MVP]

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 said:
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 said:
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 said:
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
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
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?


 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top