In this article I will explain how to show the up and down arrows during sorting of gridview columns using asp.net.
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.
<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> |
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; } } } } |
<connectionStrings> <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 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 Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
15 comments :
hi suresh ji very nice keep it up.
subhash jakhar
its not showing any but the data should not sort please put sample download
its not showing error but the data should not sorting please put the sample download
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
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---------------------
Good one sir...Thank u for giving this...
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?
Hi all ,
Can u please help me to sorting implemented in window application in c#
Thanks in advance
Hi its brilliant post thqqq very much for this post suresh,thqqqqq
Hi,
You have not handle Sorting event. It will give The GridView 'gvEmpDetails' fired event Sorting which wasn't handled error message
Download Sample code should be here
good
hi..
really It is very Good site folearning
asp.net.
good