Show Summery in the GridView’s Footer in ASP.Net ( C# )

April 30, 2012 // In: .Net, ASP.Net, C#
Information is displayed at the bottom of the GridView. Gridview has property to show footer at the bottom. And you can show some information or summary in it. If you are going to generate some financial or any analytical  report that contains numeric values and you may want to show the sum at the bottom of the GridView, You obviously will enable Footer in the GridView, but how to calculate sum and show in the footer?  Its very easy.

gridview 300x283 Show Summery in the GridViews Footer in ASP.Net ( C# )

Lets Start!,
For the Purpose of tutorial I’ve created a Database and a table in it. I have named the table as tbl_shops_detail (shopid, shopname, purchase, sale, profit) and bind the table with GridView.
First of All go to the properties of GridView and Set ShowFooter property to true.

showfooter 300x261 Show Summery in the GridViews Footer in ASP.Net ( C# )

Here is The Designer Code

<asp:gridview ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="Dotted"
BorderWidth="1px" CellPadding="4" ForeColor="#333333" ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<sortedascendingcellstyle backcolor="#F5F7FB" />
<sortedascendingheaderstyle backcolor="#6D95E1" />
<sorteddescendingcellstyle backcolor="#E9EBEF" />
<sorteddescendingheaderstyle backcolor="#4870BE" />
<Columns>
<asp:TemplateField HeaderText="Shop ID">
<ItemTemplate>
<asp:Label ID="lblsid" runat="server" Text='<%# Eval("ShopId")%>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shop Name">
<ItemTemplate>
<asp:Label ID="lblShopName" runat="server" Text='<%# Eval("ShopName") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTSN" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase">
<ItemTemplate>
<asp:Label ID="lblpurchase" runat="server" Text='<%# Eval("purchase") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotalpurchase" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sale">
<ItemTemplate>
<asp:Label ID="lblsale" runat="server" Text='<%# Eval("sale") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotalsale" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Profit">
<ItemTemplate>
<asp:Label ID="lblprofit" runat="server" Text='<%# Eval("profit") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotalprofit" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>

In the above example I’m using TemplateField inside the Gridview. And within the TemplateField there are two templates one is ItemTemplate and other is FooterTemplate.
The Label inside the ItemTemplate are bounded. And Labels Inside FooterTemplate are left blank.The Page_Load function of page binds data to  the Grid
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack()){
DataBind();
}
}
private void DataBind()
{
SqlConnection conn = new SqlConnection(@"Server=.\SQLEXPRESS;Database=sharpcoders;uid=sa;pwd=sa;");
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = new SqlCommand("select * from tbl_Shops_detail", conn);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

To calculate Total we need variable that will store the sum. The variables should be declared outside the RowDataBound event.
decimal totalsale = 0, totalpurchase = 0, totalprofit = 0;
RowDataBound event raised for every Row including Header and Footer. So I applied Condition for Calculation only on DataRow. And Display the Sum only in the FooterRow.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
Label lblpurchase = (Label)e.Row.FindControl("lblpurchase");
Label lblsale = (Label)e.Row.FindControl("lblsale");
Label lblprofit = (Label)e.Row.FindControl("lblprofit");
if (lblpurchase.Text != string.Empty)
{
totalpurchase += decimal.Parse(lblpurchase.Text);
}
if (lblsale.Text != string.Empty)
{
totalsale += decimal.Parse(lblsale.Text.Replace(",", ""));
}
if (lblprofit.Text != string.Empty)
{
totalprofit += decimal.Parse(lblprofit.Text.Replace(",", ""));
}
}
catch
{ }
}
if (e.Row.RowType == DataControlRowType.Footer)
{
try
{
Label lbltotalpurchase = (Label)e.Row.FindControl("lbltotalpurchase");
Label lbltotalsale = (Label)e.Row.FindControl("lbltotalsale");
Label lbltotalprofit = (Label)e.Row.FindControl("lbltotalprofit");
lbltotalpurchase.Text = Convert.ToString(totalpurchase);
lbltotalsale.Text = Convert.ToString(totalsale);
lbltotalprofit.Text = Convert.ToString(totalprofit);

}
catch
{ }
}
}

Thats It…:-)

Introduction to C# (C Sharp) Class
Send SMS using AT Commands via GSM Modem/Phone in C# (C Sharp)
You might also likeclose