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

output
Figure 2

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