Monday, 11 June 2012

how to calculate sum of gridview columns in .net

Here, we are going to calculate the total value of a perticular column in GridView1.



As you can see in above fig.. there is QUANTITY, RATE, AMOUNT columns are there..
Suppose we have a requirement where we want to calculate the total number QUANTITY for BE No.- BOE-1.
For that reason, i have written bellow code in ASP.NET(C#)

decimal a=0, b=0;   // declaring variable
for (int i = 0; i < (gridview1.Rows.Count); i++)     //Counting total number of rows in GridView1
{

    a = Convert.ToDecimal(gridview1.Rows[i].Cells[7].Text.ToString());
    b = b + a; //storing total qty into variable

}

Note-
1)  Convert.ToDecimal(gridview1.Rows[i].Cells[7].Text.ToString()); //here i have given "Rows[i].Cells[7].Text" Becouse, i want to sum quantity and its index is  7.

http://www.codeproject.com/Questions/271994/how-to-calculate-sum-of-gridview-columns-in-net





 

Now, i am going to show you, how to calculate sum of perticular column value and show it in gridview..

1) Just put this code on to your .aspx page

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ACODE"
AutoGenerateColumns = "false" onrowdatabound="GridView1_RowDataBound"
ShowFooter="True">
<FooterStyle BackColor ="AliceBlue" />
<Columns>
<asp:BoundField DataField="ACODE" HeaderText="Product ID" />
<asp:BoundField DataField="PRD_NAME" HeaderText="Product Name" />
<%-- <asp:BoundField DataField="QTY" HeaderText="Quantity" />--%>
<asp:TemplateField HeaderText = "Quantity">

<ItemTemplate>
<asp:Label ID="LblQty" runat="server" Text='<%# Bind("QTY") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:Label ID="LblSumQty" runat="server" Text="1"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BorderStyle="None" />
</asp:GridView>

2) Now assign datasource of the gridview. and set its showfooter property to true from property window.

3) copy this code on to your aspx.cs page's RowDataBound event of your gridview.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dtview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl1 = (Label)e.Row.Cells[2].FindControl("LblQty");
SQty = SQty + Convert.ToInt32(lbl1.Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total : ";

e.Row.Cells[2].Text = Convert.ToString(SQty);
}
}

this will calculate the total qty and display in gridview..



  

To know more about, how to retrieve data from database in gridview.
Please click on this link http://midotnetexp.blogspot.in/2012/10/how-to-retrieve-data-in-datagridview.html

4 comments:

  1. Find subtotals in gridview

    http://asp.net-informations.com/gridview/gridview-subtotal.htm

    ling

    ReplyDelete
  2. Lots of Thanks...........

    ReplyDelete
  3. very informative blog and useful article thank you for sharing with us , keep posting
    .NET Online Course Hyderabad

    ReplyDelete