Hi Ishwar,
Refering the below article i have created an example. Check this example. Now please take its reference and correct your code.
DataBase
For this sample i am making use of Microsoft's Northwind Database. You can download it from here.
Download and install Northwind Database
HTML
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/ScrollableGridPlugin.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#<%=GridView1.ClientID %>').Scrollable({
ScrollHeight: 300
});
});
</script>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True"
OnSorting="GridView1_Sorting">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" SortExpression="CustomerId" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
</Columns>
</asp:GridView>
</div>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = GetData();
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["tables"] = dt;
}
}
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT CustomerId,ContactName,Country FROM Customers";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string SortDir = string.Empty;
DataTable dt = new DataTable();
dt = ViewState["tables"] as DataTable;
{
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new DataView(dt);
sortedView.Sort = e.SortExpression + " " + SortDir;
GridView1.DataSource = sortedView;
GridView1.DataBind();
for (int i = 0; i < GridView1.Columns.Count; i++)
{
string lbText = ((LinkButton)GridView1.HeaderRow.Cells[i].Controls[0]).Text;
if (lbText.ToLower() == e.SortExpression.ToLower())
{
TableCell tableCell = GridView1.HeaderRow.Cells[i];
Image img = new Image();
img.ImageUrl = (SortDir == "Asc") ? "~/Images/asc.gif" : "~/Images/desc.gif";
tableCell.Controls.Add(new LiteralControl(" "));
tableCell.Controls.Add(img);
}
}
}
}
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = GetData()
GridView1.DataSource = dt
GridView1.DataBind()
ViewState("tables") = dt
End If
End Sub
Private Function GetData() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT CustomerId,ContactName,Country FROM Customers"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Public Property dir As SortDirection
Get
If ViewState("dirState") Is Nothing Then
ViewState("dirState") = SortDirection.Ascending
End If
Return CType(ViewState("dirState"), SortDirection)
End Get
Set(ByVal value As SortDirection)
ViewState("dirState") = value
End Set
End Property
Protected Sub GridView1_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs)
Dim SortDir As String = String.Empty
Dim dt As DataTable = New DataTable
dt = CType(ViewState("tables"), DataTable)
If (dir = SortDirection.Ascending) Then
dir = SortDirection.Descending
SortDir = "Desc"
Else
dir = SortDirection.Ascending
SortDir = "Asc"
End If
Dim sortedView As DataView = New DataView(dt)
sortedView.Sort = e.SortExpression & " " & SortDir
GridView1.DataSource = sortedView
GridView1.DataBind()
Dim i As Integer = 0
Do While (i < GridView1.Columns.Count)
Dim lbText As String = CType(GridView1.HeaderRow.Cells(i).Controls(0), LinkButton).Text
If (lbText.ToLower = e.SortExpression.ToLower) Then
Dim tableCell As TableCell = GridView1.HeaderRow.Cells(i)
Dim img As Image = New Image
img.ImageUrl = "~/Images/asc.gif"
img.ImageUrl = If((SortDir = "Asc"), "~/Images/asc.gif", "~/Images/desc.gif")
tableCell.Controls.Add(New LiteralControl(" "))
tableCell.Controls.Add(img)
End If
i = (i + 1)
Loop
End Sub
Screenshot