Ep

Ep

Sorting ASP.Net Gridview using List Example

Sorting Asp.Net Gridview using List of

This asp.net tutorial demonstrates asp.net gridview sorting with list. For example you need to figure out how to sort asp.net gridview with multiple columns (String, DateTime etc. data types) which is bound to a generic list of custom objects or Sorting a gridview when databinding a collection or list of objects. Find the code snippet below :-
gridview sorting list datasource,gridview sorting example c#,gridview templatefield sorting,gridview sorting example asp.net c#,gridview sorting not working,implement sorting in gridview,
sorting asp net gridview using list unity
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Implement sorting in gridview in asp.net</title>
    <style type="text/css">
        table.table-style-two {
            font-family: verdana, arial, sans-serif;
            font-size: 11px;
            color: #333333;
            border-width: 1px;
            border-color: #3A3A3A;
            border-collapse: collapse;
        }

            table.table-style-two th {
                border-width: 1px;
                padding: 8px;
                border-style: solid;
                border-color: #517994;
                background-color: #B2CFD8;
            }

            table.table-style-two tr:hover td {
                background-color: #DFEBF1;
            }

            table.table-style-two td {
                border-width: 0px;
                padding: 8px;
                border-style: solid;
                border-color: #517994;
                background-color: #ffffff;
            }
    </style>
</head>

<body>
    <form id="form1" runat="server">
        <h1 style="color: gray">Sorting a GridView bound to a list of custom generic Objects</h1>
        <div>
            <asp:GridView ID="gvPerson" DataKeyNames="Id" class="table-style-two" Font-Size="13px" runat="server" AllowSorting="true" AutoGenerateColumns="false" OnSorting="gvPerson_Sorting">
                <SelectedRowStyle BorderColor="#E2E7F2" BackColor="#E2E7F2"></SelectedRowStyle>
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Person Id" HeaderStyle-ForeColor="Black" SortExpression="Id" />
                    <asp:BoundField DataField="Name" HeaderText="Person Name" HeaderStyle-ForeColor="Black" SortExpression="Name" />
                    <asp:BoundField DataField="Address" HeaderText="Person Address" HeaderStyle-ForeColor="Black" SortExpression="Address" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>

</html>

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

public partial class _Default : System.Web.UI.Page
{
    List<Person> lstPerson = null;
    Person objPerson = new Person();
    protected void Page_Load(object sender, EventArgs e)
    {
        lstPerson = objPerson.GetPersonList();
        if (!IsPostBack)
        {
            gvPerson.DataSource = lstPerson;
            gvPerson.DataBind();
           
        }
    }

    protected void gvPerson_Sorting(object sender, GridViewSortEventArgs e)
    {
        string Sortdir = GetSortingOrder(e.SortExpression);
        string SortExp = e.SortExpression;
        var list = objPerson.GetPersonList();
        if (Sortdir == "ASC")
        {
            list = Sort<Person>(list, SortExp, SortDirection.Ascending);
        }
        else
        {
            list = Sort<Person>(list, SortExp, SortDirection.Descending);
        }
        gvPerson.DataSource = list;
        gvPerson.DataBind();
    }

    private string GetSortingOrder(string column)
    {
        string sortDirection = "ASC";
        string sortExpression = ViewState["SortExpression"] as string;
      
        if (sortExpression != null)
        {
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;              

                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
                else
                { sortDirection = "ASC"; }
            }
        }

        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;
        return sortDirection;
    }

    public List<Person> Sort<TKey>(List<Person> list, string sortBy, SortDirection direction)
    {
        PropertyInfo property = list.GetType().GetGenericArguments()[0].GetProperty(sortBy);
        if (direction == SortDirection.Ascending)
        {
            return list.OrderBy(e => property.GetValue(e, null)).ToList<Person>();
        }
        else
        {
            return list.OrderByDescending(e => property.GetValue(e, null)).ToList<Person>();
        }
    }
}
Sorting ASP.Net Gridview using List Example Reviewed by Ravi Kumar on 6:35:00 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.