Hi All,
I want to show total in gridview's footer. If I had template field or bounded field in my gridview I could have done it before but in my scenario it's different.
I need to show total for all columns except the first column because it doesn't have integer it's a string.
Below is my gridview.
and here I am binding the gridview
protected void Bind_Grid()
{
DateTime Date; DateTime.TryParseExact(lblDate.Text, "dd/MM/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out Date);
Business_Logic bal = new Business_Logic();
DataTable dt = new DataTable();
dt = bal.TMS_Report(Date.Date, lblEmpCode.Text);
if (dt.Rows.Count > 0)
{
GVReport.DataSource = dt;
GVReport.DataBind();
GVReport.Visible = true;
}
}
Gridview is binding perfectly. Hpw can I show the total in footer?
see my bounded gridview in below image


Upendra Pratap ShahiPosted Jul 20, 2015, 7:10 AM
protected void BindGridviewFooter()
{
//Creating a dataset
DataSet ds = new DataSet();
DataTable dt;
DataRow dr;
DataColumn pName;
DataColumn pQty;
DataColumn pPrice;
DataColumn pCategory;
DataColumn d2,d3,d4,d5,d6,d7,d8,d9,d10;
//create an object of datatable
dt = new DataTable();
//creating column of datatable with datatype
pName = new DataColumn("Product_Name", Type.GetType("System.String"));
pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
d2 = new DataColumn("D2", Type.GetType("System.Int32"));
d3 = new DataColumn("D3", Type.GetType("System.Int32"));
d4 = new DataColumn("D4", Type.GetType("System.Int32"));
d5 = new DataColumn("D5", Type.GetType("System.Int32"));
d6 = new DataColumn("D6", Type.GetType("System.Int32"));
d7 = new DataColumn("D7", Type.GetType("System.Int32"));
d8 = new DataColumn("D8", Type.GetType("System.Int32"));
d9= new DataColumn("D9", Type.GetType("System.Int32"));
d10 = new DataColumn("D10", Type.GetType("System.Int32"));
pCategory = new DataColumn("Category", Type.GetType("System.String"));
//bind data table columns in datatable
dt.Columns.Add(pName);
dt.Columns.Add(pQty);
dt.Columns.Add(pPrice);
dt.Columns.Add(d2);
dt.Columns.Add(d3);
dt.Columns.Add(d4);
dt.Columns.Add(d5);
dt.Columns.Add(d6);
dt.Columns.Add(d7);
dt.Columns.Add(d8);
dt.Columns.Add(d9);
dt.Columns.Add(d10);
dt.Columns.Add(pCategory);
//creating data row and assiging the value to columns of datatable
dr = dt.NewRow();
dr["Product_Name"] = "Product 1";
dr["Quantity"] = 2;
dr["Price"] = 200;
dr["D2"] = 13;
dr["D3"] = 13;
dr["D4"] = 13;
dr["D5"] = 13;
dr["D6"] = 13;
dr["D7"] = 13;
dr["D8"] = 13;
dr["D9"] = 13;
dr["D10"] = 13;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 2";
dr["Quantity"] = 5;
dr["Price"] = 480;
dr["D2"] = 13;
dr["D3"] = 13;
dr["D4"] = 13;
dr["D5"] = 13;
dr["D6"] = 13;
dr["D7"] = 13;
dr["D8"] = 13;
dr["D9"] = 13;
dr["D10"] = 13;
dr["Category"] = "Cat2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 3";
dr["Quantity"] = 8;
dr["Price"] = 100;
dr["D2"] = 13;
dr["D3"] = 13;
dr["D4"] = 13;
dr["D5"] = 13;
dr["D6"] = 13;
dr["D7"] = 13;
dr["D8"] = 53;
dr["D9"] = 73;
dr["D10"] = 13;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 4";
dr["Quantity"] = 2;
dr["Price"] = 500;
dr["D2"] = 13;
dr["D3"] = 13;
dr["D4"] = 13;
dr["D5"] = 13;
dr["D6"] = 23;
dr["D7"] = 13;
dr["D8"] = 13;
dr["D9"] = 13;
dr["D10"] = 13;
dr["Category"] = "Cat2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 5";
dr["Quantity"] = 8;
dr["Price"] = 100;
dr["D2"] = 13;
dr["D3"] = 13;
dr["D4"] = 13;
dr["D5"] = 13;
dr["D6"] = 23;
dr["D7"] = 13;
dr["D8"] = 13;
dr["D9"] = 13;
dr["D10"] = 13;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 6";
dr["Quantity"] = 3;
dr["Price"] = 90;
dr["D2"] = 13;
dr["D3"] = 13;
dr["D4"] = 13;
dr["D5"] = 13;
dr["D6"] = 13;
dr["D7"] = 53;
dr["D8"] = 13;
dr["D9"] = 43;
dr["D10"] = 13;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 7";
dr["Quantity"] = 18;
dr["Price"] = 1100;
dr["D2"] = 13;
dr["D3"] = 13;
dr["D4"] = 14;
dr["D5"] = 13;
dr["D6"] = 13;
dr["D7"] = 14;
dr["D8"] = 13;
dr["D9"] = 13;
dr["D10"] = 13;
dr["Category"] = "Cat2";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
//here add code for column total sum and show in footer
int total = 0; ;
GridView1.FooterRow.Cells[0].Text = "Total";
GridView1.FooterRow.Cells[1].Font.Bold = true;
GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Left;
for (int k = 1; k < dt.Columns.Count-1; k++)
{
total = dt.AsEnumerable().Sum(row => row.Field<Int32>(dt.Columns[k].ToString()));
GridView1.FooterRow.Cells[k].Text = total.ToString();
GridView1.FooterRow.Cells[k].Font.Bold = true;
GridView1.FooterRow.BackColor = System.Drawing.Color.Beige;
}
}
OutPut-Upendra Pratap ShahiPosted Jul 21, 2015, 2:37 AM
Gaurav KumarPosted Jul 21, 2015, 2:33 AM
Upendra Pratap ShahiPosted Jul 21, 2015, 2:21 AM
Upendra Pratap ShahiPosted Jul 20, 2015, 7:39 AM
Gaurav KumarPosted Jul 20, 2015, 7:36 AM
Upendra Pratap ShahiPosted Jul 20, 2015, 7:11 AM
Gaurav KumarPosted Jul 20, 2015, 6:58 AM
Upendra Pratap ShahiPosted Jul 20, 2015, 6:53 AM
for (int k = 1; k < GVReport.Columns.Count; k++) {
total = dt.AsEnumerable().Sum(row => row.Field(dt.Columns[k].ToString()));
GVReport.FooterRow.Cells[k].Text = total.ToString();
total = dt.AsEnumerable().Sum(row => row.Field(dt.Columns[k].ToString()));
GVReport.FooterRow.Cells[k].Text = total.ToString();
GVReport.FooterRow.Cells[k].Font.Bold = true;
GVReport.FooterRow.BackColor = System.Drawing.Color.Beige; }
Gaurav KumarPosted Jul 20, 2015, 6:49 AM
Upendra Pratap ShahiPosted Jul 20, 2015, 6:45 AM
for (int k = 1; k < dt.Columns.Count; k++)
{
total = dt.AsEnumerable().Sum(row => row.Field<Int32>(dt.Columns[k].ToString()));
GridView1.FooterRow.Cells[k].Text = total.ToString();
GridView1.FooterRow.Cells[k].Font.Bold = true;
GridView1.FooterRow.BackColor = System.Drawing.Color.Beige;
}
Gaurav KumarPosted Jul 20, 2015, 6:44 AM
Upendra Pratap ShahiPosted Jul 20, 2015, 6:41 AM
ShowFooter="true"
Gaurav KumarPosted Jul 20, 2015, 6:36 AM
Upendra Pratap ShahiPosted Jul 20, 2015, 3:51 AM
Design Page-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewColumnAdd.aspx.cs" Inherits="GridViewColumnAdd" %>
DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>title>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-BackColor="Wheat" ShowFooter="true">
asp:GridView>
div>
form>
body>
html>
Your design looks like this-Code behind Page-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class GridViewColumnAdd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
BindGridviewFooter();
}
}
protected void BindGridviewFooter()
{
//Creating a dataset
DataSet ds = new DataSet();
DataTable dt;
DataRow dr;
DataColumn pName;
DataColumn pQty;
DataColumn pPrice;
DataColumn pCategory;
//create an object of datatable
dt = new DataTable();
//creating column of datatable with datatype
pName = new DataColumn("Product_Name", Type.GetType("System.String"));
pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
pCategory = new DataColumn("Category", Type.GetType("System.String"));
//bind data table columns in datatable
dt.Columns.Add(pName);
dt.Columns.Add(pQty);
dt.Columns.Add(pPrice);
dt.Columns.Add(pCategory);
//creating data row and assiging the value to columns of datatable
dr = dt.NewRow();
dr["Product_Name"] = "Product 1";
dr["Quantity"] = 2;
dr["Price"] = 200;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 2";
dr["Quantity"] = 5;
dr["Price"] = 480;
dr["Category"] = "Cat2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 3";
dr["Quantity"] = 8;
dr["Price"] = 100;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 4";
dr["Quantity"] = 2;
dr["Price"] = 500;
dr["Category"] = "Cat2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 5";
dr["Quantity"] = 8;
dr["Price"] = 100;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 6";
dr["Quantity"] = 3;
dr["Price"] = 90;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 7";
dr["Quantity"] = 18;
dr["Price"] = 1100;
dr["Category"] = "Cat2";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
//here add code for column total sum and show in footer
int total = 0; ;
GridView1.FooterRow.Cells[0].Text = "Total";
GridView1.FooterRow.Cells[1].Font.Bold = true;
GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Left;
for (int k = 1; k < dt.Columns.Count-1; k++)
{
total = dt.AsEnumerable().Sum(row => row.Field<Int32>(dt.Columns[k].ToString()));
GridView1.FooterRow.Cells[k].Text = total.ToString();
GridView1.FooterRow.Cells[k].Font.Bold = true;
GridView1.FooterRow.BackColor = System.Drawing.Color.Beige;
}
}
}