Sunday, March 29, 2015

Display Total (Sum) of GridView Columns in Footer using ASP.Net C# and VB.Net

Introduction:
Here I will explain how display Total in GridView's footer using ASP.NET. 
Total (Grand Total) in GridView Footer in ASP.Net using C# and VB.Net. The GridView has paging enabled and the sum of values of a particular column on each page will be displayed in the GridView Footer Row as Grand Total.

 The below code in your aspx page
 The HTML Markup consists of an ASP.Net GridView. The ShowFooter property is set to true so that the GridView’s Footer Row is displayed.
Paging has been enabled for the GridView and OnPageIndexChanging event handler has been assigned.



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" ShowFooter="true">
<Columns>
    <asp:BoundField DataField="EmpID" HeaderText="Employee ID" ItemStyle-Width="60" />
    <asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" ItemStyle-Width="210" />
    <asp:BoundField DataField="Salary" HeaderText="Salary" ItemStyle-Width="60" DataFormatString="{0:N2}"
        ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>

Code behind file .cs


protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
        DataTable dt = new DataTable();
        dt.Columns.Add("
EmpID", typeof(Int32));
        dt.Columns.Add("
EmployeeName", typeof(string));
        dt.Columns.Add("Education", typeof(string));
        dt.Columns.Add("Location", typeof(string));
        dt.Columns.Add("
Salary", typeof(decimal));
        dt.Rows.Add(31, "Muhammed Asad", "BCIT", "Karachi", 85);
        dt.Rows.Add(32, "Muhammed Ali", "Msc", "Karachi", 812);
        dt.Rows.Add(33, "Faraz Ahmed", "MS", "Lahore", 67);
        dt.Rows.Add(34, "Asma", "MBA", "Hyderabad", 34);
        dt.Rows.Add(36, "Faheem Shah", "MBBS", "Karachi", 74);
        dt.Rows.Add(37, "Asif Raza", "B.Tech", "Hyderabad", 55);
        dt.Rows.Add(38, "Daniel", "CA", "Karachi", 853);
        dt.Rows.Add(31, "Muhammed Asad", "BCIT", "Karachi", 85);
        dt.Rows.Add(32, "Muhammed Ali", "Msc", "Karachi", 812);
        dt.Rows.Add(33, "Faraz Ahmed", "MS", "Lahore", 67);
        dt.Rows.Add(34, "Asma", "MBA", "Hyderabad", 34);
        dt.Rows.Add(36, "Faheem Shah", "MBBS", "Karachi", 74);
        dt.Rows.Add(37, "Asif Raza", "B.Tech", "Hyderabad", 55);
        dt.Rows.Add(38, "Daniel", "CA", "Karachi", 853);
        dt.Rows.Add(31, "Muhammed Asad", "BCIT", "Karachi", 85);
        dt.Rows.Add(32, "Muhammed Ali", "Msc", "Karachi", 812);
        dt.Rows.Add(33, "Faraz Ahmed", "MS", "Lahore", 67);
        dt.Rows.Add(34, "Asma", "MBA", "Hyderabad", 34);
        dt.Rows.Add(36, "Faheem Shah", "MBBS", "Karachi", 74);
        dt.Rows.Add(37, "Asif Raza", "B.Tech", "Hyderabad", 55);
        dt.Rows.Add(38, "Daniel", "CA", "Karachi", 853);
        GridView1.DataSource = dt;
        GridView1.DataBind();
 
               
 
                    //Calculate Sum and display in Footer Row
                    decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("Salary"));
                    GridView1.FooterRow.Cells[1].Text = "Total";
                    GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
                    GridView1.FooterRow.Cells[2].Text = total.ToString("N2");
                }
           

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}
 
}
 
 

No comments:

Post a Comment