Wednesday 8 October 2014

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

GridView Footer  Total



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();

        }
    }

}




                   


                          
                               























                                             
                                                                     

0 comments:

Post a Comment