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

 
using System.Collections.Generic;
/// <summary>
/// Summary description for Student
/// </summary>
public class Student
{
public Student()
{
//
// TODO: Add constructor logic here
//
}
public string RollNo { get; set; }
public string StudentName{get;set;}
public List<Student> GetStudentList()
{
List<Student> lstStudent=new List<Student>();
Student objStudent1=new Student();
objStudent1.RollNo="23";
objStudent1.StudentName="Ravi Shrivastav";
lstStudent.Add(objStudent1);
Student objStudent2 = new Student();
objStudent2.RollNo = "46";
objStudent2.StudentName = "Nitin Kumar";
lstStudent.Add(objStudent2);
Student objStudent3 = new Student();
objStudent3.RollNo = "37";
objStudent3.StudentName = "Neha Gupta";
lstStudent.Add(objStudent3);
Student objStudent4 = new Student();
objStudent4.RollNo = "36";
objStudent4.StudentName = "Markandey Pathak";
lstStudent.Add(objStudent4);
Student objStudent5 = new Student();
objStudent5.RollNo = "56";
objStudent5.StudentName = "Rahul Porwal";
lstStudent.Add(objStudent5);
Student objStudent6 = new Student();
objStudent6.RollNo = "40";
objStudent6.StudentName = "Piyush Pandey";
lstStudent.Add(objStudent6);
return lstStudent;
}
}

Default.aspx

 <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>Sorting in GridView using list</h2>
<div style="font-size: 14px; color: #000;">
<asp:GridView ID="gdvStudent" runat="server"
AllowSorting="true" 
AutoGenerateColumns="false" onsorting="gdvStudent_Sorting" Width="300px" >
<Columns>
<asp:BoundField DataField="RollNo"  HeaderText="Roll No"  SortExpression="RollNo" />
<asp:BoundField DataField="StudentName" HeaderText="Student Name"  SortExpression="StudentName" />
</Columns>
</asp:GridView>
</div>
</asp:Content>
 


Default.aspx.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
List<Student> lstStudent = null;
Student objStudent = new Student();
protected void Page_Load(object sender, EventArgs e)
{
lstStudent = objStudent.GetStudentList();
if (!IsPostBack)
{
gdvStudent.DataSource = lstStudent;
gdvStudent.DataBind();
}
}
/// <summary>
/// Gridview sorting event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvStudent_Sorting(object sender, GridViewSortEventArgs e)
{
string Sortdir = GetSortDirection(e.SortExpression);
string SortExp = e.SortExpression;
var list = objStudent.GetStudentList();
if (Sortdir == "ASC")
{
list = Sort<Student>(list, SortExp, SortDirection.Ascending);
}
else
{
list = Sort<Student>(list, SortExp, SortDirection.Descending);
}
this.gdvStudent.DataSource = list;
this.gdvStudent.DataBind();
}
/// <summary>
/// GEt Sorting direction
/// </summary>
/// <param name="column"></param>
/// <returns></returns>
private string GetSortDirection(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";
}
}
}
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
/// <summary>
/// Sort function
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="list"></param>
/// <param name="sortBy"></param>
/// <param name="direction"></param>
/// <returns></returns>
public List<Student> Sort<TKey>(List<Student> 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<Student>();
}
else
{
return list.OrderByDescending(e => property.GetValue(e, null)).ToList<Student>();
}
}
}

Sorting using lsit in gridview