Add Row Total To GridView Footer in ASP.Net

We will learn here how to bind controls with data without a database and how to handle total sum of a column on controls.

Also read:

Initial Chamber

Step 1

Open your Visual Studio and create an empty website then provide a suitable name such as GridViewColumnAdd.

Step 2

In Solution Explorer you will get your empty website, then add some web forms.

GridViewColumnAdd (your empty website). Right-click and select Add New Item Web Form. Name it GridViewColumnAdd.aspx.

Design Chamber

Step 3

Open the GridViewColumnAdd.aspx file and write some code for the design of the application.

Choose the control from the toolbox and provide your design page like:
  1. <form id="form1" runat="server">  
  2.    <div>  
  3.       <asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-BackColor="Wheat" ShowFooter="true">  
  4.       </asp:GridView>  
  5.    </div>  
  6. </form>  
Here I've enabled the "ShowFooter" property of GridView to show the total sum of a column in the footer.

Here I've designed the GridView Control and used some property as in the following.

Design Page
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewColumnAdd.aspx.cs" Inherits="GridViewColumnAdd" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-BackColor="Wheat" ShowFooter="true">  
  13.             </asp:GridView>  
  14.         </div>  
  15.     </form>  
  16. </body>  
  17. </html>  
Your design looks as in the following:

design
Figure 1

Code Chamber

Step 4

In the code chamber we will write some code so that our application works.

Add the following namespaces to the namespace section of your code behind page:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
Now your page looks as in the following.

Code behind Page
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9.   
  10. public partial class GridViewColumnAdd : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         if (!IsPostBack) {  
  15.             BindGridviewFooter();  
  16.         }  
  17.     }  
  18.     protected void BindGridviewFooter()  
  19.     {  
  20.   
  21.         //Creating a dataset  
  22.         DataSet ds = new DataSet();  
  23.         DataTable dt;  
  24.         DataRow dr;  
  25.         DataColumn pName;  
  26.         DataColumn pQty;  
  27.         DataColumn pPrice;  
  28.         DataColumn pCategory;  
  29.         //create an object of datatable  
  30.         dt = new DataTable();  
  31.         //creating column of datatable with datatype  
  32.         pName = new DataColumn("Product_Name", Type.GetType("System.String"));  
  33.         pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));  
  34.         pPrice = new DataColumn("Price", Type.GetType("System.Int32"));  
  35.         pCategory = new DataColumn("Category", Type.GetType("System.String"));  
  36.         //bind data table columns in datatable  
  37.         dt.Columns.Add(pName);  
  38.         dt.Columns.Add(pQty);  
  39.         dt.Columns.Add(pPrice);  
  40.         dt.Columns.Add(pCategory);  
  41.         //creating data row and assiging the value to columns of datatable  
  42.         dr = dt.NewRow();  
  43.         dr["Product_Name"] = "Product 1";  
  44.         dr["Quantity"] = 2;  
  45.         dr["Price"] = 200;  
  46.         dr["Category"] = "Cat1";  
  47.         dt.Rows.Add(dr);  
  48.   
  49.         dr = dt.NewRow();  
  50.         dr["Product_Name"] = "Product 2";  
  51.         dr["Quantity"] = 5;  
  52.         dr["Price"] = 480;  
  53.         dr["Category"] = "Cat2";  
  54.         dt.Rows.Add(dr);  
  55.   
  56.         dr = dt.NewRow();  
  57.         dr["Product_Name"] = "Product 3";  
  58.         dr["Quantity"] = 8;  
  59.         dr["Price"] = 100;  
  60.         dr["Category"] = "Cat1";  
  61.         dt.Rows.Add(dr);  
  62.   
  63.         dr = dt.NewRow();  
  64.         dr["Product_Name"] = "Product 4";  
  65.         dr["Quantity"] = 2;  
  66.         dr["Price"] = 500;  
  67.         dr["Category"] = "Cat2";  
  68.         dt.Rows.Add(dr);  
  69.   
  70.         dr = dt.NewRow();  
  71.         dr["Product_Name"] = "Product 5";  
  72.         dr["Quantity"] = 8;  
  73.         dr["Price"] = 100;  
  74.         dr["Category"] = "Cat1";  
  75.         dt.Rows.Add(dr);  
  76.   
  77.         dr = dt.NewRow();  
  78.         dr["Product_Name"] = "Product 6";  
  79.         dr["Quantity"] = 3;  
  80.         dr["Price"] = 90;  
  81.         dr["Category"] = "Cat1";  
  82.         dt.Rows.Add(dr);  
  83.   
  84.         dr = dt.NewRow();  
  85.         dr["Product_Name"] = "Product 7";  
  86.         dr["Quantity"] = 18;  
  87.         dr["Price"] = 1100;  
  88.         dr["Category"] = "Cat2";  
  89.         dt.Rows.Add(dr);  
  90.   
  91.         ds.Tables.Add(dt);  
  92.         GridView1.DataSource = ds.Tables[0];  
  93.         GridView1.DataBind();  
  94.         //here add code for column total sum and show in footer  
  95.         int total = 0; ;  
  96.         GridView1.FooterRow.Cells[0].Text = "Total";  
  97.         GridView1.FooterRow.Cells[1].Font.Bold = true;  
  98.         GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Left;  
  99.         for (int k = 1; k < dt.Columns.Count-1; k++)  
  100.         {  
  101.             total = dt.AsEnumerable().Sum(row => row.Field<Int32>(dt.Columns[k].ToString()));  
  102.             GridView1.FooterRow.Cells[k].Text = total.ToString();  
  103.             GridView1.FooterRow.Cells[k].Font.Bold = true;  
  104.             GridView1.FooterRow.BackColor = System.Drawing.Color.Beige;  
  105.         }  
  106.     }  
  107.       
  108. }  
Output

output
Figure 2

I hope you liked this. Have a good day. Thank you for reading.


Similar Articles