Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 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 asp.net

Feb 23, 2011
Introduction:

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


Description:

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="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Sort Gridview Cloumns with Up and Down Arrows</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:11pt;
font-weight:normal;
color:black;
width:350px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<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" /> 
<Columns>
<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" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsEmpdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from EmployeeSalary" >
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
 
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;
}
else
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


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


Demo


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

Get the latest Asp.net, C#.net, 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

17 comments :

Subhash Jakhar 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...

Unknown 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?

Rahul sharma 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

har gokul govind said...
This comment has been removed by the author.
Mubashir Shaikh said...

Hi,
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...

good

Daya Ram said...

hi..
really It is very Good site folearning
asp.net.

Anonymous said...

good

munir sheikh said...

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

jas said...

suresh bahi engineer bana k chhodeoge....

Give your Valuable Comments

Other Related Posts

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