Hi SantoshC,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView ID="gvCustomers" runat="server" CssClass="table table-bordered table-striped"
AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" OnSorting="gvCustomers_Sorting"
PageSize="10" DataKeyNames="CustomerID" OnPageIndexChanging="gvCustomers_PageIndexChanging"
PagerStyle-HorizontalAlign="Center" PagerSettings-Mode="NumericFirstLast"
PagerSettings-Position="TopAndBottom" EmptyDataText="No Data Found" ShowHeaderWhenEmpty="true">
<Columns>
<asp:TemplateField HeaderText="#" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" SortExpression="CustomerId">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Eval("CustomerId") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="ContactName">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<ItemTemplate>
<asp:Label ID="lblFeedbackName" runat="server" Text='<%# Eval("City") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<ItemTemplate>
<asp:Label ID="lblFeedbackName" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NumericFirstLast" />
<PagerStyle BorderStyle="None" CssClass="pagination-ys" />
</asp:GridView>
Feedback Class
C#
public class Feedback
{
public string CustomerId { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
VB.Net
Public Class Feedback
Public Property CustomerId As String
Public Property ContactName As String
Public Property City As String
Public Property Country As String
End Class
Feedbacks Class
C#
public class Feedbacks
{
public List<Feedback> GetAllFeedbacks(string StoreId)
{
NORTHWINDEntities entities = new NORTHWINDEntities();
List<Feedback> customers = new List<Feedback>();
if (!string.IsNullOrEmpty(StoreId))
{
customers = entities.Customers
.Where(x => x.CustomerID == StoreId)
.Select(x => new Feedback
{
CustomerId = x.CustomerID,
ContactName = x.ContactName,
City = x.City,
Country = x.Country
}).ToList();
}
else
{
customers = entities.Customers
.Select(x => new Feedback
{
CustomerId = x.CustomerID,
ContactName = x.ContactName,
City = x.City,
Country = x.Country
}).ToList();
}
return customers;
}
}
VB.Net
Public Class Feedbacks
Public Function GetAllFeedbacks(ByVal StoreId As String) As List(Of Feedback)
Dim entities As NORTHWINDEntities = New NORTHWINDEntities()
Dim customers As List(Of Feedback) = New List(Of Feedback)()
If Not String.IsNullOrEmpty(StoreId) Then
customers = entities.Customers _
.Where(Function(x) x.CustomerID = StoreId) _
.Select(Function(x) New Feedback With {
.CustomerId = x.CustomerID,
.ContactName = x.ContactName,
.City = x.City,
.Country = x.Country
}).ToList()
Else
customers = entities.Customers _
.Select(Function(x) New Feedback With {
.CustomerId = x.CustomerID,
.ContactName = x.ContactName,
.City = x.City,
.Country = x.Country
}).ToList()
End If
Return customers
End Function
End Class
Namespaces
C#
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
VB.Net
Imports System.Collections.Generic
Imports System.Data
Imports System.Linq
Imports System.Reflection
Code
C#
Feedbacks objController = new Feedbacks();
List<Feedback> listFeedback = new List<Feedback>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetAllFeedbacks();
}
}
private void GetAllFeedbacks()
{
listFeedback = objController.GetAllFeedbacks(Convert.ToString(Session["StoreId"]));
BindDataToGrid();
}
public void BindDataToGrid()
{
if (listFeedback.Count != 0)
{
gvCustomers.DataSource = listFeedback;
gvCustomers.DataBind();
}
else
{
DataTable dt = new DataTable();
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
string sortDirection = ViewState["SortDirection"] != null ? ViewState["SortDirection"].ToString() : "ASC";
string sortExpression = ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : "CustomerId";
var list = objController.GetAllFeedbacks(Convert.ToString(Session["StoreId"]));
if (sortDirection == "ASC")
{
listFeedback = Sort<Feedback>(list, sortExpression, SortDirection.Ascending);
}
else
{
listFeedback = Sort<Feedback>(list, sortExpression, SortDirection.Descending);
}
BindDataToGrid();
}
protected void gvCustomers_Sorting(object sender, GridViewSortEventArgs e)
{
string Sortdir = GetSortDirection(e.SortExpression);
string SortExp = e.SortExpression;
var list = objController.GetAllFeedbacks(Convert.ToString(Session["StoreId"]));
if (Sortdir == "ASC")
{
listFeedback = Sort<Feedback>(list, SortExp, SortDirection.Ascending);
}
else
{
listFeedback = Sort<Feedback>(list, SortExp, SortDirection.Descending);
}
BindDataToGrid();
}
public List<Feedback> Sort<TKey>(List<Feedback> 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<Feedback>();
}
else
{
return list.OrderByDescending(e => property.GetValue(e, null)).ToList<Feedback>();
}
}
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";
}
else
{
sortDirection = "ASC";
}
}
}
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
VB.Net
Private objController As Feedbacks = New Feedbacks()
Private listFeedback As List(Of Feedback) = New List(Of Feedback)()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
GetAllFeedbacks()
End If
End Sub
Private Sub GetAllFeedbacks()
listFeedback = objController.GetAllFeedbacks(Convert.ToString(Session("StoreId")))
BindDataToGrid()
End Sub
Public Sub BindDataToGrid()
If listFeedback.Count <> 0 Then
gvCustomers.DataSource = listFeedback
gvCustomers.DataBind()
Else
Dim dt As DataTable = New DataTable()
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Protected Sub gvCustomers_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvCustomers.PageIndex = e.NewPageIndex
Dim sortDirection As String = If(ViewState("SortDirection") IsNot Nothing, ViewState("SortDirection").ToString(), "ASC")
Dim sortExpression As String = If(ViewState("SortExpression") IsNot Nothing, ViewState("SortExpression").ToString(), "CustomerId")
Dim list = objController.GetAllFeedbacks(Convert.ToString(Session("StoreId")))
If sortDirection = "ASC" Then
listFeedback = Sort(Of Feedback)(list, WebControls.SortExpression, sortDirection.Ascending)
Else
listFeedback = Sort(Of Feedback)(list, WebControls.SortExpression, sortDirection.Descending)
End If
BindDataToGrid()
End Sub
Protected Sub gvCustomers_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
Dim Sortdir As String = GetSortDirection(e.SortExpression)
Dim SortExp As String = e.SortExpression
Dim list = objController.GetAllFeedbacks(Convert.ToString(Session("StoreId")))
If Sortdir = "ASC" Then
listFeedback = Sort(Of Feedback)(list, SortExp, SortDirection.Ascending)
Else
listFeedback = Sort(Of Feedback)(list, SortExp, SortDirection.Descending)
End If
BindDataToGrid()
End Sub
Public Function Sort(Of TKey)(ByVal list As List(Of Feedback), ByVal sortBy As String, ByVal direction As SortDirection) As List(Of Feedback)
Dim [property] As PropertyInfo = list.GetType().GetGenericArguments()(0).GetProperty(sortBy)
If direction = SortDirection.Ascending Then
Return list.OrderBy(Function(e) [property].GetValue(e, Nothing)).ToList(Of Feedback)()
Else
Return list.OrderByDescending(Function(e) [property].GetValue(e, Nothing)).ToList(Of Feedback)()
End If
End Function
Private Function GetSortDirection(ByVal column As String) As String
Dim sortDirection As String = "ASC"
Dim sortExpression As String = TryCast(ViewState("SortExpression"), String)
If sortExpression IsNot Nothing Then
If sortExpression = column Then
Dim lastDirection As String = TryCast(ViewState("SortDirection"), String)
If (lastDirection IsNot Nothing) AndAlso (lastDirection = "ASC") Then
sortDirection = "DESC"
Else
sortDirection = "ASC"
End If
End If
End If
ViewState("SortDirection") = sortDirection
ViewState("SortExpression") = column
Return sortDirection
End Function
Screenshot
![](https://i.imgur.com/RvFnBye.gif)