-->

04 February 2020

Asp.Net GridView Displaying Column Summaries

  Asp.Net CS By Example       04 February 2020


 GridView Displaying Column Summaries 

 Suppose we want to display a column total at the bottom of a column. In that case, we can handle the GridView RowDataBound event to sum the values in a column and display the summary in the column footer.

 For example, A GridView control that displays a summary column representing the total of all Employee Salaries.

 The GridView control uses a TemplateField to represent the TotalSalary column. The TemplateField includes a <FooterTemplate> that contains a Label control. The grid_RowDataBound() method displays the total of the All Employee Salary in this Label control.


 The DataNavigateUrlFields property accepts a comma-separated list of column names. we can use multiple placeholders in DataNavigateUrlFormatString.

  Code: GridView18_DisplaySummary.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView18_DisplaySummary.aspx.cs"
    Inherits="LearnAsp.Net.ControlDemo.GridView.GridView18_DisplaySummary" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Displaying Column Summaries</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
            Employee Info</h1>
        <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" CellPadding="4" 
        ShowFooter="true" OnRowDataBound="grid_RowDataBound" ForeColor="#333333">
            <Columns>
                <asp:BoundField DataField="empno" HeaderText="Emp No" />
                <asp:BoundField DataField="ename" HeaderText="Name" />
                <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <%# Eval("sal", "{0:c}")%>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblSummary" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#5D7B01" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
        <br />
        <asp:Label ID="lblSelectRow" runat="server" />
    </div>
    </form>
</body>
</html>

  Code: GridView18_DisplaySummary.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;

namespace LearnAsp.Net.ControlDemo.GridView
{
    public partial class GridView18_DisplaySummary : System.Web.UI.Page
    {
        private decimal _OfficeTotalSalary = 0;
        static string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
        private OracleConnection objConn = new OracleConnection(strConn);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { GridBind(); }
        }

        protected void GridBind()
        {
            String query = " select * from emp ";
            OracleCommand objCmd= new OracleCommand(query, objConn);
            DataTable dt = new DataTable();
            objConn.Open();
            OracleDataAdapter oda = new OracleDataAdapter(objCmd);
            oda.Fill(dt);
            objConn.Close();

            if (dt.Rows.Count > 0)
            {
                grid.DataSource = dt;
                grid.DataBind();
            }
            else
            {
                grid.DataSource = dt.NewRow();
                grid.DataBind();
                int columncount = grid.Rows[0].Cells.Count;
                grid.Rows[0].Cells.Clear();
                grid.Rows[0].Cells.Add(new TableCell());
                grid.Rows[0].Cells[0].ColumnSpan = columncount;
                grid.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
        protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal OfficeSalary = (decimal)DataBinder.Eval(e.Row.DataItem, "sal");
                _OfficeTotalSalary += OfficeSalary;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblSummary = (Label)e.Row.FindControl("lblSummary");
                lblSummary.Text = String.Format("Total: {0:c}", _OfficeTotalSalary);
            }
        }
    }
}

  Output:
logoblog

Thanks for reading Asp.Net GridView Displaying Column Summaries

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.