Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to create total in gridview?
Posted
Comments
Felipe Gentil 22-Mar-12 10:35am    
What do you mean by create a total? Do you want to sum a column value?
dineshdena 22-Mar-12 10:42am    
yes i want to sum the column values in gridview

Sum your needed value
C#
int sum = 0;
foreach(DataGridViewRow item in grid.Rows)
{
   sum += item.Cells[INDEX_YOUR_VALUE].Value;
}


Add a last row with the sum value
C#
grid.Rows.Add(YOUR_PARAMETERS_BEFORE, sum);


Is this what you are looking for?
Regards.
 
Share this answer
 
Hello

If you want to have a sum column:
I prefer to create a new property in my class named Sum
If you can modify the database Alter the table or the view create a new Sum Column (Computed Column)
http://msdn.microsoft.com/en-us/library/ms191250.aspx[^]
But if you can't modify the database, then you can make it's class manually.

If you want to display Sum Total in the Footer of the GridView Control:
This articles may help:

1. http://www.asp.net/web-forms/tutorials/data-access/custom-formatting/displaying-summary-information-in-the-gridview-s-footer-vb[^]

2. http://techbrij.com/831/display-total-gridview-footer-row-linq-asp-net[^]

3. http://aspalliance.com/782_CodeSnip_How_to_Display_Sum_Total_in_the_Footer_of_the_GridView_Control[^]
 
Share this answer
 
v2
Comments
ProEnggSoft 22-Mar-12 20:59pm    
Good links. Particularly link 1 of www.asp.net is excellent. +5
Shahin Khorshidnia 22-Mar-12 21:11pm    
Thank you very much :)
dineshdena 24-Mar-12 1:40am    
i tried this code but im getting error anyone plz help me

void BindGrid()
{
SqlDataAdapter add = new SqlDataAdapter("select Total from tblQuotationreg",con);

DataTable dt = new DataTable();
add.Fill(dt);
GridView1.Columns[6].FooterText = (from row in dt.AsEnumerable()select row.Field<int>("Total")).Sum().ToString();
GridView1.DataSource = dt;
GridView1.DataBind();
}

its showing error::
error points here:::(select row.Field<int>("Total"))
error is:::Specified cast is not valid.
you can show the total at the footer of the gridview.

use footeritemitemplate and handle the rowdatabound event of gridview.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900