Click here to Skip to main content
14,699,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Frnds !!

I have a Gridview on my webform. Finally i have filled it with database.

I this Gridview Records will be filled with Form, This forms it contains Textboxes and Button.
and these Textboxes value will be filled in gridview when submit button clicks after entering text and numbers into textbox...so its Inserting Well.

But now i need Total Amount at bottom.

Example Gridview Display
========================
Name        Item         Amount
-----       ----         ------  
Raju       Laptop         15000
Varun      HandyCam       12000
Teja       Iphone         18000
Ranjith     PC            22000 

Now We need Total Amount Automatically below.

Please show me, how to do total amount in Gridview.

Thanks.
Posted
Updated 21-May-12 21:48pm
v2
Comments
John Orendt 22-May-12 2:38am
   
Please show the code that populates the DataGridView.
Anuja Pawar Indore 22-May-12 2:48am
   
Formatting of ques done

int sum = 0;
foreach(DataRow dr in datatable.Rows)
{
    sum = sum + Convert.ToInt32(dr["Amount"]);
}


sum variable will contain the total of all the values.Use this 'sum' where ever u need.

chk this out ....
   
v2
You can use CellValidated[^] event and place your calculations in there.

Also refer following threads:

This article demonstrates how to develop client side calculations in a GridView, and calculation expressions are configurable at the DB side:
Database driven client side calculations in GridView[^]

Using JavaScript/JQuery:
Calculating GridView total using JavaScript/JQuery[^]

How To Add client side calculation for gridview column[^]
How to Display Sum Total in the Footer of the GridView Control[^]

GridView Examples for ASP.NET: Displaying Summary Data in the Footer[^]
Displaying Summary Information in the GridView's Footer[^]

Check similar discussion on following link:
Calculate Total Value In Gridvew In ASP.net C#[^]

Hope you get some needful from it!
   
v2
You should use FooterRow of gridview then
after databind you can write this line

GridView.FooterRow.Cells[2].Text = ds.Tables[0].Compute("sum(Amount)", "").ToString();
   
Hi,

yes you can solve your problem using row databound event of gridview
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
     
          int amount=int.Parse( e.Row.Cells[2].Text) ;

//your calculation

    }

  }


Hope it helps u
best Luck
Happy Coding:)
   
v2
function CalculateTax(fixedtotal)
  {
    var taxgrid = document.getElementById('<%=gvAttribute.ClientID %>');
    var taxip = taxgrid.getElementsByTagName('input');
    var taxamount = 0*1;

    for(i = 0; i < taxip.length; i+= 2)

    {

        var sign = taxip[i].value;

        var tax = taxip[i+1].value;

        taxamount = parseFloat(taxamount) + (sign =='+' ? 1 : -1)* parseFloat(tax);

    }

    return parseFloat(fixedtotal) + parseFloat(taxamount);

  }



Here it is your model for more information visit <a href="http://stackoverflow.com/questions/6288504/how-to-calculate-the-sum-of-gridview-columns-using-javascript"><b>
<b><b></b></b>http://stackoverflow.com/questions/6288504/how-to-calculate-the-sum-of-gridview-columns-using-javascript</b></a>[<a href="http://stackoverflow.com/questions/6288504/how-to-calculate-the-sum-of-gridview-columns-using-javascript" target="_blank" title="New Window">^</a>]
   
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblqy = (Label)e.Row.FindControl("lblqty");
int qty = Int32.Parse(lblqy.Text );
total = total + qty;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotalqty = (Label)e.Row.FindControl("lblTotalqty");
lblTotalqty.Text = total.ToString();
}
}

More Info : GridView Total on Footer

Walsh.
   

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