Sum(Total) of GridView Columns Total in Footer Row Display in Asp.Net C#

GridView Footer Display  Total Amount

Gridview Column Values  Total(Sum) Display  in Gridview Footer Row


                  Download Coding


                     Html Coding 

<html xmlns="">
<head id="Head1" runat="server">
    <form id="form1" runat="server">

            <asp:GridView ID="GridView1" runat="server"
                 OnRowDataBound="GridView1_RowDataBound" ShowFooter="True">
                    <asp:TemplateField HeaderText="Name">
                            <asp:Label ID="Label1"
                                 Text='<%# Eval("name") %>'></asp:Label>
                    <asp:TemplateField HeaderText="Mark1">
                            <asp:Label ID="lblMark1"
                            <asp:Label ID="Label2"
                                Text='<%# Eval("mark1") %>'></asp:Label>
                    <asp:TemplateField HeaderText="Mark2">
                            <asp:Label ID="lblMark2"
                            <asp:Label ID="Label4"                                runat="server" Text='<%# Eval("mark2") %>'></asp:Label>




C# Coding 

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 gridfooter : System.Web.UI.Page

    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;

    int total1 = 0;
    int total2 = 0;

    public void dbcon()
        string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);


    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    protected void bind11()
        query = "select * from student";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
            GridView1.DataSource = ds;
            GridView1.DataSource = ds;
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            total1 += (DataBinder.Eval(e.Row.DataItem, "mark1") != System.DBNull.Value) ? Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "mark1")) : 0;

            total2 += (DataBinder.Eval(e.Row.DataItem, "mark2") != System.DBNull.Value) ? Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "mark2")) : 0;

        if (e.Row.RowType == DataControlRowType.Footer)

            Label lblMarkOne = (Label)e.Row.FindControl("lblMark1");

            lblMarkOne.Text = total1.ToString();

            Label lblMarkSecond = (Label)e.Row.FindControl("lblMark2");

            lblMarkSecond.Text = total2.ToString();



First - Add New Form -  Select  GridView From Toolbox 

Select  - GridView - Click -  Edit Columns -Fields Window Open - Remove Auto Generated  Fields

Next - Add Template Fields  for Requireds

Next - Template Field Name Change  Header Text Finally Click  Ok  button

                          Display the  Added Column Fields

Next - Select - Edit Templates - Select  the Column

Next -  Label Select  to Item Template  From Name Column Name 

Next - Select Label  Edit DataBinding - binding the Database Fields(Eval("name"))

Next - Select Label  Edit DataBinding - binding the Database Fields(Eval("mark1"))

           Next - Select  Label Button - Change ID Name(lblMark1)

Next - Select Label  Edit DataBinding - binding the Database Fields(Eval("mark2"))

Next - Select  Label Button - Change ID Name(lblMark2)

Next - Select  - End Template Editing

Next - GridView  DataBinding   But Does't Show Footer Row 

Select - Gridview Go To F4 Property Window - Change ShowFooter=True

Show the Footer Row

Next - Insert Values To Database Table  Fields

Next - Double Click or Go To Coding Parts

Next  -  Add NameSpaces For Library  Files Call

Database Connections  Add

Next - Page Load Function  Give IsPostBack

And Table Data Selected Query Given

Select - GridView -  Go To Property F4  Double Click - Event Symbol -  Select RoeDataBound - Double Click

Next -  Publically Add Two Variable  in Integer
(int total1 = 0 ,int total2 =0 )

Next - Go To Data Row Bound  -  Total  Values convert to Integer 

Next -  Call  Label  Id For Footer Row Total

Next - label  Id Call to  Footer Row  

Next - Run The Program F5 -  Mark1 & Mark2 Values Sum Display

If   Database Table Values Null or Deleted   



Post a Comment