{"id":600,"date":"2014-08-08T13:45:32","date_gmt":"2014-08-08T13:45:32","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=600"},"modified":"2014-08-08T13:45:32","modified_gmt":"2014-08-08T13:45:32","slug":"show-summery-in-the-gridviews-footer-in-asp-net-c","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/show-summery-in-the-gridviews-footer-in-asp-net-c\/","title":{"rendered":"Show Summery in the GridView\u2019s Footer in ASP.Net ( C# )"},"content":{"rendered":"<p>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 \u00a0report that contains numeric values and you may want to show the sum at the bottom of the GridView, You\u00a0obviously\u00a0will enable Footer in the GridView, but how to calculate sum and show in the footer? \u00a0Its very easy.<\/p>\n<p>Lets Start!,<br \/>\nFor the Purpose of tutorial I\u2019ve created a Database and a table in it. I have named the table as <strong>tbl_shops_detail (shopid, shopname, purchase, sale, profit)\u00a0<\/strong>and bind the table with <strong>GridView<\/strong>.<br \/>\nFirst of All go to the properties of GridView and Set ShowFooter property to true.<\/p>\n<div>\n<div style=\"clear: both; text-align: center;\"><a class=\"cboxElement\" href=\"http:\/\/sharp-coders.com\/wp-content\/uploads\/2012\/04\/showfooter.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-115 colorbox-16\" title=\"showfooter\" src=\"http:\/\/sharp-coders.com\/wp-content\/uploads\/2012\/04\/showfooter-300x261.jpg\" alt=\"showfooter 300x261 Show Summery in the GridViews Footer in ASP.Net ( C# )\" width=\"300\" height=\"261\" \/><\/a><\/div>\n<p>Here is The Designer Code<br \/>\n<code><br \/>\n&lt;asp:gridview ID=\"GridView1\" runat=\"server\" AutoGenerateColumns=\"False\" BorderStyle=\"Dotted\"<br \/>\nBorderWidth=\"1px\" CellPadding=\"4\" ForeColor=\"#333333\" ShowFooter=\"True\" onrowdatabound=\"GridView1_RowDataBound\"&gt;<br \/>\n&lt;AlternatingRowStyle BackColor=\"White\" \/&gt;<br \/>\n&lt;EditRowStyle BackColor=\"#2461BF\" \/&gt;<br \/>\n&lt;FooterStyle BackColor=\"#507CD1\" Font-Bold=\"True\" ForeColor=\"White\" \/&gt;<br \/>\n&lt;HeaderStyle BackColor=\"#507CD1\" Font-Bold=\"True\" ForeColor=\"White\" \/&gt;<br \/>\n&lt;PagerStyle BackColor=\"#2461BF\" ForeColor=\"White\" HorizontalAlign=\"Center\" \/&gt;<br \/>\n&lt;RowStyle BackColor=\"#EFF3FB\" \/&gt;<br \/>\n&lt;SelectedRowStyle BackColor=\"#D1DDF1\" Font-Bold=\"True\" ForeColor=\"#333333\" \/&gt;<br \/>\n&lt;sortedascendingcellstyle backcolor=\"#F5F7FB\" \/&gt;<br \/>\n&lt;sortedascendingheaderstyle backcolor=\"#6D95E1\" \/&gt;<br \/>\n&lt;sorteddescendingcellstyle backcolor=\"#E9EBEF\" \/&gt;<br \/>\n&lt;sorteddescendingheaderstyle backcolor=\"#4870BE\" \/&gt;<br \/>\n&lt;Columns&gt;<br \/>\n&lt;asp:TemplateField HeaderText=\"Shop ID\"&gt;<br \/>\n&lt;ItemTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lblsid\" runat=\"server\" Text='&lt;%# Eval(\"ShopId\")%&gt;' \/&gt;<br \/>\n&lt;\/ItemTemplate&gt;<br \/>\n&lt;FooterTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lblTotal\" runat=\"server\" \/&gt;<br \/>\n&lt;\/FooterTemplate&gt;<br \/>\n&lt;\/asp:TemplateField&gt;<br \/>\n&lt;asp:TemplateField HeaderText=\"Shop Name\"&gt;<br \/>\n&lt;ItemTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lblShopName\" runat=\"server\" Text='&lt;%# Eval(\"ShopName\") %&gt;' \/&gt;<br \/>\n&lt;\/ItemTemplate&gt;<br \/>\n&lt;FooterTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lblTSN\" runat=\"server\" \/&gt;<br \/>\n&lt;\/FooterTemplate&gt;<br \/>\n&lt;\/asp:TemplateField&gt;<br \/>\n&lt;asp:TemplateField HeaderText=\"Purchase\"&gt;<br \/>\n&lt;ItemTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lblpurchase\" runat=\"server\" Text='&lt;%# Eval(\"purchase\") %&gt;' \/&gt;<br \/>\n&lt;\/ItemTemplate&gt;<br \/>\n&lt;FooterTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lbltotalpurchase\" runat=\"server\" \/&gt;<br \/>\n&lt;\/FooterTemplate&gt;<br \/>\n&lt;\/asp:TemplateField&gt;<br \/>\n&lt;asp:TemplateField HeaderText=\"Sale\"&gt;<br \/>\n&lt;ItemTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lblsale\" runat=\"server\" Text='&lt;%# Eval(\"sale\") %&gt;' \/&gt;<br \/>\n&lt;\/ItemTemplate&gt;<br \/>\n&lt;FooterTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lbltotalsale\" runat=\"server\" \/&gt;<br \/>\n&lt;\/FooterTemplate&gt;<br \/>\n&lt;\/asp:TemplateField&gt;<br \/>\n&lt;asp:TemplateField HeaderText=\"Profit\"&gt;<br \/>\n&lt;ItemTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lblprofit\" runat=\"server\" Text='&lt;%# Eval(\"profit\") %&gt;' \/&gt;<br \/>\n&lt;\/ItemTemplate&gt;<br \/>\n&lt;FooterTemplate&gt;<br \/>\n&lt;asp:Label ID=\"lbltotalprofit\" runat=\"server\" \/&gt;<br \/>\n&lt;\/FooterTemplate&gt;<br \/>\n&lt;\/asp:TemplateField&gt;<br \/>\n&lt;\/Columns&gt;<br \/>\n&lt;\/asp:gridview&gt;<br \/>\n<\/code><\/p>\n<\/div>\n<p>In the above example I\u2019m using TemplateField inside the Gridview. And within the TemplateField there are two templates one is ItemTemplate and other is FooterTemplate.<br \/>\nThe Label inside the ItemTemplate are bounded. And Labels Inside FooterTemplate are left blank.The Page_Load function of page binds data to \u00a0the Grid<br \/>\n<code><span style=\"color: #0b5394;\">protected void Page_Load(object sender, EventArgs e)<br \/>\n{<br \/>\nif(!isPostBack()){<br \/>\nDataBind();<br \/>\n}<br \/>\n}<br \/>\nprivate void DataBind()<br \/>\n{<br \/>\nSqlConnection conn = new SqlConnection(@\"Server=.\\SQLEXPRESS;Database=sharpcoders;uid=sa;pwd=sa;\");<br \/>\nSqlDataAdapter da = new SqlDataAdapter();<br \/>\nDataTable dt = new DataTable();<br \/>\nda.SelectCommand = new SqlCommand(\"select * from tbl_Shops_detail\", conn);<br \/>\nda.Fill(dt);<br \/>\nGridView1.DataSource = dt;<br \/>\nGridView1.DataBind();<br \/>\n}<\/span><\/code><br \/>\nTo calculate Total we need variable that will store the sum. The variables should be declared outside the RowDataBound event.<br \/>\n<code><span style=\"color: #3d85c6;\">decimal<\/span><span style=\"color: #073763;\"> totalsale = 0, totalpurchase = 0, totalprofit = 0;<\/span><\/code><br \/>\nRowDataBound 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.<br \/>\n<code><span style=\"color: #073763;\">protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)<br \/>\n{<br \/>\nif (e.Row.RowType == DataControlRowType.DataRow)<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\nLabel lblpurchase = (Label)e.Row.FindControl(\"lblpurchase\");<br \/>\nLabel lblsale = (Label)e.Row.FindControl(\"lblsale\");<br \/>\nLabel lblprofit = (Label)e.Row.FindControl(\"lblprofit\");<br \/>\nif (lblpurchase.Text != string.Empty)<br \/>\n{<br \/>\ntotalpurchase += decimal.Parse(lblpurchase.Text);<br \/>\n}<br \/>\nif (lblsale.Text != string.Empty)<br \/>\n{<br \/>\ntotalsale += decimal.Parse(lblsale.Text.Replace(\",\", \"\"));<br \/>\n}<br \/>\nif (lblprofit.Text != string.Empty)<br \/>\n{<br \/>\ntotalprofit += decimal.Parse(lblprofit.Text.Replace(\",\", \"\"));<br \/>\n}<br \/>\n}<br \/>\ncatch<br \/>\n{ }<br \/>\n}<br \/>\nif (e.Row.RowType == DataControlRowType.Footer)<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\nLabel lbltotalpurchase = (Label)e.Row.FindControl(\"lbltotalpurchase\");<br \/>\nLabel lbltotalsale = (Label)e.Row.FindControl(\"lbltotalsale\");<br \/>\nLabel lbltotalprofit = (Label)e.Row.FindControl(\"lbltotalprofit\");<br \/>\nlbltotalpurchase.Text = Convert.ToString(totalpurchase);<br \/>\nlbltotalsale.Text = Convert.ToString(totalsale);<br \/>\nlbltotalprofit.Text = Convert.ToString(totalprofit);<\/span><br \/>\n}<br \/>\ncatch<br \/>\n{ }<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\nThats It\u2026:-)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u00a0report that contains numeric values and you may want to show the sum at the bottom [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":601,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[32,9],"tags":[],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/600"}],"collection":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/comments?post=600"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media\/601"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}