views:

32

answers:

1

In an asp.net page I have five gridviews listing some numbers. In the header of each gridview I want to summarize the numbers in the listings. So if the listing contains rows with numbers 1,2,3 the title in the header would be 6.

I can't find a way to do it in my query for the gridview (SELECT number,SUM(number) FROM tbl, is not working).

It seems like a bad way to connect to the database again to sum these numbers, as I'm already connecting five times to get the data for the gridviews.

What is the best solution for doing this?

Thanks!

+1  A: 

What you can do is in the Page Pre_Render event, loop through the rows as such. GridView1:

int Total = 0;
GridView1.HeaderRow.Cells[0].Text = GetTotals();
private String GetTotals()
{
Foreach(GridviewRow row in GridView1.Rows)
{
Total += int.Parse(((Label)row.FindControl("RowValue")).Text);
}
return Total.ToString();
}

Should work for you.

+1 while not pretty this would work. Ms put some restrictions in the design of the gridview, particularly when doing paging. I say this because paging would affect other solutions (like using linq2sql to retrieve the same) and require work arounds to hook with object datasources.
eglasius