ASP.NET GridView Tutorial
Lesson 240 of 301 · How to include sort arrows when sorting an asp.net gridview control – Part 49 · Database
How to include sort arrows when sorting an asp.net gridview control – Part 49
Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists
Link for text version of this video
http://csharp-video-tutorials.blogspot.com/2013/04/how-to-include-sort-arrows-when-sorting.html
In this video we will discuss about including sort arrows, when sorting an asp.net gridview control. When the data is sorted in ascending order, we want to show UP ARROW and if the data is sorted in descending order we want to show DOWN ARROW.
Please watch Part 48 using the link below, before proceeding with this video.
We will be making use of RowCreated event. Generate event handler method.
Copy and paste the following code in WebForm1.aspx.cs
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (GridView1.Attributes[“CurrentSortField”] != null && GridView1.Attributes[“CurrentSortDirection”] != null)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell tableCell in e.Row.Cells)
{
if (tableCell.HasControls())
{
LinkButton sortLinkButton = null;
if (tableCell.Controls[0] is LinkButton)
{
sortLinkButton = (LinkButton)tableCell.Controls[0];
}
if (sortLinkButton != null && GridView1.Attributes[“CurrentSortField”] == sortLinkButton.CommandArgument)
{
Image image = new Image();
if (GridView1.Attributes[“CurrentSortDirection”] == “ASC”)
{
image.ImageUrl = “~/Images/up_arrow.png”;
}
else
{
image.ImageUrl = “~/Images/down_arrow.png”;
}
tableCell.Controls.Add(new LiteralControl(” “));
tableCell.Controls.Add(image);
}
}
}
}
}
}