Monday, September 19, 2011

GridView Paging,Sorting,Salary Total Display in Footer Row With Out Bound Fields



Source Code

<table class="style1">
        <tr>
            <td align="center">
                <asp:GridView ID="GridView1" runat="server"
                    BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
                    CellPadding="2" ForeColor="Black" GridLines="None"
                    onrowdatabound="GridView1_RowDataBound" ShowFooter="True"
                    AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging"
                    PageSize="4" AllowSorting="True" onsorting="GridView1_Sorting"
                    AutoGenerateColumns="false">
                    <FooterStyle BackColor="Tan" />
                    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
                        HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                    <HeaderStyle BackColor="Tan" Font-Bold="True" />
                    <AlternatingRowStyle BackColor="PaleGoldenrod" />
                    <Columns>                      
                        <asp:BoundField DataField="id" HeaderText="ID" />
                        <asp:BoundField DataField="saltotal" HeaderText="SalTotal"  SortExpression="saltotal" />                  
                    <asp:TemplateField>                  
                    <FooterTemplate>
                        <asp:Label ID="lblTotal" runat="server"></asp:Label>
                    </FooterTemplate>
                    </asp:TemplateField>
                    </Columns>
                </asp:GridView>
             
            </td>
        </tr>
    </table>



C# Code:


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;

public partial class GVSortPageingTotal : System.Web.UI.Page
{
    MySqlConnection cn = new MySqlConnection("server=localhost;database=gg;uid=root;pwd=root");
    decimal grdTotal = 0;
    static DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillgrid();
        }
    }
    void fillgrid()
    {
        MySqlDataAdapter da = new MySqlDataAdapter("select * from tbltotal", cn);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            grdTotal = 0;
            // Below one line code With Bound Fields
            //decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "saltotal"));
            int i = 0;
            for (; i < GridView1.Rows.Count; i++)
            {
                decimal rowTotal = Convert.ToDecimal(GridView1.Rows[i].Cells[1].Text);
                grdTotal = grdTotal + rowTotal;
            }
            if (i < GridView1.PageSize)
            {
                decimal rowTotal = Convert.ToDecimal(dt.Rows[i].ItemArray[1].ToString());
                grdTotal = grdTotal + rowTotal;
            }
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lbl = (Label)e.Row.FindControl("lblTotal");
            lbl.Text = grdTotal.ToString("c");
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        fillgrid();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, GridViewSortDirection.ToString());
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, GridViewSortDirection.ToString());
        }
    }

    private void SortGridView(string sortExpression, string p)
    {
        DataView dv = new DataView(dt);
        dv.Sort = sortExpression;
        GridView1.DataSource = dv;
        GridView1.DataBind();
    }
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sort"] == null)
                ViewState["sort"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sort"];
        }
        set { ViewState["sort"] = value; }
    }
}

No comments:

Post a Comment