Sorting in grid view using Generic List

Sorting in grid view using Generic List

In this example we create a class student and bind grid view using generic list of student type and add storing on generic list to bind grid view.

Student.cs

  1. using System.Collections.Generic;  
  2. /// <summary>  
  3. /// Summary description for Student  
  4. /// </summary>  
  5. public class Student  
  6. {  
  7.     public Student()  
  8.     {  
  9.         //  
  10.         // TODO: Add constructor logic here  
  11.         //  
  12.     }  
  13.     public string RollNo { getset; }  
  14.     public string StudentName { getset; }  
  15.     public List<Student> GetStudentList()  
  16.     {  
  17.         List<Student> lstStudent = new List<Student>();  
  18.         Student objStudent1 = new Student();  
  19.         objStudent1.RollNo = "23";  
  20.         objStudent1.StudentName = "Ravi Shrivastav";  
  21.         lstStudent.Add(objStudent1);  
  22.         Student objStudent2 = new Student();  
  23.         objStudent2.RollNo = "46";  
  24.         objStudent2.StudentName = "Nitin Kumar";  
  25.         lstStudent.Add(objStudent2);  
  26.         Student objStudent3 = new Student();  
  27.         objStudent3.RollNo = "37";  
  28.         objStudent3.StudentName = "Neha Gupta";  
  29.         lstStudent.Add(objStudent3);  
  30.         Student objStudent4 = new Student();  
  31.         objStudent4.RollNo = "36";  
  32.         objStudent4.StudentName = "Markandey Pathak";  
  33.         lstStudent.Add(objStudent4);  
  34.         Student objStudent5 = new Student();  
  35.         objStudent5.RollNo = "56";  
  36.         objStudent5.StudentName = "Rahul Porwal";  
  37.         lstStudent.Add(objStudent5);  
  38.         Student objStudent6 = new Student();  
  39.         objStudent6.RollNo = "40";  
  40.         objStudent6.StudentName = "Piyush Pandey";  
  41.         lstStudent.Add(objStudent6);  
  42.         return lstStudent;  
  43.     }  
  44. }  

Default.aspx
  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"  
  2.     CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  3.   
  4. <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">  
  5. </asp:Content>  
  6. <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">  
  7.     <h2>Sorting in GridView using list</h2>  
  8.     <div style="font-size: 14px; color: #000;">  
  9.         <asp:GridView ID="gdvStudent" runat="server"  
  10.             AllowSorting="true"  
  11.             AutoGenerateColumns="false" OnSorting="gdvStudent_Sorting" Width="300px">  
  12.             <Columns>  
  13.                 <asp:BoundField DataField="RollNo" HeaderText="Roll No" SortExpression="RollNo" />  
  14.                 <asp:BoundField DataField="StudentName" HeaderText="Student Name" SortExpression="StudentName" />  
  15.             </Columns>  
  16.         </asp:GridView>  
  17.     </div>  
  18. </asp:Content>  
Default.aspx.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web.UI.WebControls;  
  5. using System.Reflection;  
  6. public partial class _Default : System.Web.UI.Page  
  7. {  
  8.     List<Student> lstStudent = null;  
  9.     Student objStudent = new Student();  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         lstStudent = objStudent.GetStudentList();  
  13.         if (!IsPostBack)  
  14.         {  
  15.             gdvStudent.DataSource = lstStudent;  
  16.             gdvStudent.DataBind();  
  17.         }  
  18.     }  
  19.     /// <summary>  
  20.     /// Gridview sorting event  
  21.     /// </summary>  
  22.     /// <param name="sender"></param>  
  23.     /// <param name="e"></param>  
  24.     protected void gdvStudent_Sorting(object sender, GridViewSortEventArgs e)  
  25.     {  
  26.         string Sortdir = GetSortDirection(e.SortExpression);  
  27.         string SortExp = e.SortExpression;  
  28.         var list = objStudent.GetStudentList();  
  29.         if (Sortdir == "ASC")  
  30.         {  
  31.             list = Sort<Student>(list, SortExp, SortDirection.Ascending);  
  32.         }  
  33.         else  
  34.         {  
  35.             list = Sort<Student>(list, SortExp, SortDirection.Descending);  
  36.         }  
  37.         this.gdvStudent.DataSource = list;  
  38.         this.gdvStudent.DataBind();  
  39.     }  
  40.     /// <summary>  
  41.     /// GEt Sorting direction  
  42.     /// </summary>  
  43.     /// <param name="column"></param>  
  44.     /// <returns></returns>  
  45.     private string GetSortDirection(string column)  
  46.     {  
  47.         string sortDirection = "ASC";  
  48.         string sortExpression = ViewState["SortExpression"as string;  
  49.         if (sortExpression != null)  
  50.         {  
  51.             if (sortExpression == column)  
  52.             {  
  53.                 string lastDirection = ViewState["SortDirection"as string;  
  54.                 if ((lastDirection != null) && (lastDirection == "ASC"))  
  55.                 {  
  56.                     sortDirection = "DESC";  
  57.                 }  
  58.             }  
  59.         }  
  60.         ViewState["SortDirection"] = sortDirection;  
  61.         ViewState["SortExpression"] = column;  
  62.         return sortDirection;  
  63.     }  
  64.     /// <summary>  
  65.     /// Sort function  
  66.     /// </summary>  
  67.     /// <typeparam name="TKey"></typeparam>  
  68.     /// <param name="list"></param>  
  69.     /// <param name="sortBy"></param>  
  70.     /// <param name="direction"></param>  
  71.     /// <returns></returns>  
  72.     public List<Student> Sort<TKey>(List<Student> list, string sortBy, SortDirection direction)  
  73.     {  
  74.         PropertyInfo property = list.GetType().GetGenericArguments()[0].GetProperty(sortBy);  
  75.         if (direction == SortDirection.Ascending)  
  76.         {  
  77.             return list.OrderBy(e => property.GetValue(e, null)).ToList<Student>();  
  78.         }  
  79.         else  
  80.         {  
  81.             return list.OrderByDescending(e => property.GetValue(e, null)).ToList<Student>();  
  82.         }  
  83.     }  
  84. }