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

                              DEMO


                      
                  Download Coding

                                 Download

                     Html Coding 


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table><tr><td>
       
        </td></tr>

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

    </table>

    </div>
    </form>
</body>

</html>


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);
        con.Open();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind11();
        }
    }
    protected void bind11()
    {
        dbcon();
        query = "select * from student";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.FooterRow.Cells.Clear();
            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   







                                               
                                                                     

0 comments:

Post a Comment