aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to show the up and down arrows during sorting of column in gridview using

Feb 23, 2011

In this article I will explain how to show the up and down arrows during sorting of gridview columns using


I have a one gridview with some data here my requirement is to show up and down arrows during sorting of columns in gridview because if I didn’t used arrows we are unable identify the sorted column in gridview and we don’t have chance to know that sorted column data is either in ascending or descending order if we use up and down arrows we can clearly show which column has sorted and whether that particular column data is in ascending or descending order.
Now design your aspx page like this

<html xmlns="">
<head id="Head1" runat="server">
<title>Sort Gridview Cloumns with Up and Down Arrows</title>
<style type="text/css">
<form id="form1" runat="server">
<asp:GridView ID="gvEmpDetails" runat="server" CssClass="Gridview"
AllowPaging="true" AllowSorting="true" PageSize="4" HeaderStyle-BackColor="#7779AF"
HeaderStyle-ForeColor="White" AutoGenerateColumns="false" ShowFooter="true"
DataSourceID="dsEmpdetails" onrowdatabound="gvEmpDetails_RowDataBound">
<PagerSettings Mode= "NumericFirstLast"  FirstPageText="First" PreviousPageText="Previous" NextPageText="Next"  LastPageText="Last" /> 
<PagerStyle BackColor="#7779AF" Font-Bold="true" ForeColor="White" /> 
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
<asp:SqlDataSource ID="dsEmpdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from EmployeeSalary" >
After completion of aspx page design write the following code in codebehind

protected void gvEmpDetails_RowDataBound(object sender, GridViewRowEventArgs e)
string imgAsc = @" <img src='AscArrow.jpg' border='0' title='Ascending' />";
string imgDes = @" <img src='DscArrow.jpg' border='0' title='Descendng' />";
if (e.Row.RowType == DataControlRowType.Header)
foreach (TableCell cell in e.Row.Cells)
LinkButton lnkbtn = (LinkButton)cell.Controls[0];
if (lnkbtn.Text == gvEmpDetails.SortExpression)
if (gvEmpDetails.SortDirection == SortDirection.Ascending)
lnkbtn.Text += imgAsc;
lnkbtn.Text += imgDes;
After that set your database connection in web.config like this because we are using this connection in our sqldatasource to get the data from database

<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings >


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Unknown said...

hi suresh ji very nice keep it up.
subhash jakhar

sathish said...

its not showing any but the data should not sort please put sample download

sathish said...

its not showing error but the data should not sorting please put the sample download

Suresh Dasari said...

hi sathish,
just now i check the code it's working fine here you need to remember one point that is your BoundField properties DataField and HeaderText should contain same text like DataField="EmpID" HeaderText="EmpID" and check whether you set AllowSorting="true" or not

vinay said...

suresh really i will browse so many sites for code.But its really such a good and sexy site that i had never seen.In this site everything will be clear.Thats what the difference between this site and another site.really good good good---------------------

Mani USS said...

Good one sir...Thank u for giving this...

SKKO said...

I want do sorting using C# codebehind,
how can I do?
Because, in my coding, I didn't use SqlDataSource,
so, I need to sort GridView using codebehind.
do u have any good references?

Unknown said...

Hi all ,

Can u please help me to sorting implemented in window application in c#

Thanks in advance

Saleem said...

Hi its brilliant post thqqq very much for this post suresh,thqqqqq

Unknown said...
This comment has been removed by the author.
Mubashir said...

You have not handle Sorting event. It will give The GridView 'gvEmpDetails' fired event Sorting which wasn't handled error message

Anonymous said...

Download Sample code should be here

Anonymous said...


Unknown said...

really It is very Good site folearning

Anonymous said...


Unknown said...

sorting is not working suresh bhai please give sample download to check it out

jas said...

suresh bahi engineer bana k chhodeoge....

Anonymous said...

Provide sample source download

Unknown said...
This comment has been removed by the author.
Unknown said...

hai suresh,i am getting an error with this code,
Error in this line :LinkButton lnkbtn = (LinkButton)cell.Controls[0];
Error is:Specified argument was out of the range of valid values.
so please help in this
thank you in advance

Unknown said...

First, the sorting doesn't work. It is giving same error referenced above that
The GridView 'GridView1' fired event Sorting which wasn't handled.

Second, please don't remove commenter's comments simply because you don't like it.

It makes you look bad because you choose which ones say nice things about you and remove the ones that don't.

This is probably going to removed too even though it is not criticizing you.

Good work though if you can fix that error.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.