Saturday 29 December 2012

Calculate total sum of columns in gridview footer using c# in Asp.net.


In this post I will explain Hoe to calculate total sum of columns in gridview footer using c# in Asp.Net.

Description:-

I have to show total sum of columns in griview footer.  So take one gridview control on your page and show footer(Initially it’s visibily is false). Create one database table with a name EmpSalary  like this.

ColumnName
DataType
EmpId
Int(primary key)
EmpName
Varchar(50)
DeptId
Int
Salary
Varchar(50)

Insert some Dummy data into the table.

Now create Design page like this.

Default.aspx:-

<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" AutoGenerateColumns="False" AllowPaging="true" PageSize="5"
            BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource1"
            ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField=" EmpId " HeaderText="EmpId" SortExpression=" EmpId " />
                <asp:BoundField DataField=" EmpName " HeaderText="EmpName"
                    SortExpression=" EmpName " />
                    <asp:TemplateField HeaderText="DeptId">
                    <ItemTemplate >
                        <asp:Label ID="lblDeptId" runat="server" Text='<%#Eval("DeptId") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotalAmount" runat="server" Text="Total Amount"></asp:Label>
                    </FooterTemplate>
                    </asp:TemplateField>
               <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate >
                        <asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Salary") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotal" runat="server" ></asp:Label>
                    </FooterTemplate>
                    </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />

        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:modiConnectionString %>"
            SelectCommand="SELECT [EmpId], [EmpName], [DeptId], [Salary] FROM [EmpSalary]">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

After  completion of design page write the code in codebehind.

Default.aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default : System.Web.UI.Page
{
    int total = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Salary"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lbltotalAmount = (Label)e.Row.FindControl("lblTotal");
            lbltotalAmount.Text = total.ToString();
        }

    }
}

Your ouput should be like this.


1 comment:

Note: only a member of this blog may post a comment.