GridView summary on Footer
In Asp.Net GridView, displaying summary data is a common report requirement. In usual situations we have to display the total value of all gridrows in the Footer row. We can include this footer row in GridView in a simple way.
Default.aspx code:-
Default.aspx.cs code:-
Default.aspx code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
Default.aspx.cs code:-
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class Default : System.Web.UI.Page { int total = 0; string connectionString = "Data Source=PS-PC\\SANTOSH;Initial Catalog=dbSantoshTest;Integrated Security=True"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { loadRecord(); } } protected void loadRecord() { SqlConnection con = new SqlConnection(connectionString); con.Open(); SqlCommand cmd = new SqlCommand("Select * from tblProduct", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); int count = ds.Tables[0].Rows.Count; con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvDetail.DataSource = ds; gvDetail.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); gvDetail.DataSource = ds; gvDetail.DataBind(); int columncount = gvDetail.Rows[0].Cells.Count; } } protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label lblqy = (Label)e.Row.FindControl("lblqty"); int qty = Int32.Parse(lblqy.Text); total = total + qty; } if (e.Row.RowType == DataControlRowType.Footer) { Label lblTotalqty = (Label)e.Row.FindControl("lblTotalqty"); lblTotalqty.Text = total.ToString(); } } }