Generic Function for GridView Sort Images

It was a requirment in one of my projects, that we have to show sort images alongwith header text, so I googled the problem and find a solution, but solution was not generic, so I did some tweaks with it and the outcome is a generic function to show sort images in your GridView header alongwith column text.

So here goes the functions...................~

C#

void GridViewSortImages(object sender, GridViewRowEventArgs e)

    {  GridView  senderGridView = (GridView) sender;         Literal space = new Literal;         space.Text = "    ";

        if (e.Row!= null && e.Row.RowType == DataControlRowType.Header)

        {

            foreach (TableCell cell in e.Row.Cells)

            {

                if (cell.HasControls)

                {

                    LinkButton button = cell.Controls[0] as LinkButton;

                    if (button!= null)

                    {

                        Image image = new Image;

                        image.ImageUrl = "default.gif";

                        if (senderGridView.SortExpression == button.CommandArgument)

                        {

                            if (senderGridView.SortDirection == SortDirection.Ascending)

                                image.ImageUrl = "asc.gif";

                            else

                                image.ImageUrl = "desc.gif";

                        }

                        cell.Controls.Add(image);

                    }

                }

            }

        }

    }

VB.NET

Sub GridViewSortImages(ByVal sender As Object, ByVal e As GridViewRowEventArgs)             Dim senderGridView As GridView = CType(sender, GridView)             Dim space As New Literal             space.Text = "    "

            If Not (e.Row Is Nothing) AndAlso e.Row.RowType = DataControlRowType.Header Then                 For Each cell As TableCell In e.Row.Cells                     If cell.HasControls Then                         Dim button As LinkButton = CType((cell.Controls(0)), LinkButton)                         If Not (button Is Nothing) Then                             Dim image As New System.Web.UI.WebControls.Image                             image.ImageUrl = "\images\clear.gif"                             If senderGridView.SortExpression = button.CommandArgument Then                                 If senderGridView.SortDirection = SortDirection.Ascending Then                                     image.ImageUrl = "\images\glyphs\arrow_down.gif"                                 Else                                     image.ImageUrl = "\images\glyphs\arrow_up.gif"                                 End If                             End If                             cell.Controls.Add(image)                         End If                     End If                 Next             End If         End Sub

You can use this code by simply binding GridView's RowCreated event.

Links:



Happy Coding:)

This article is part of the GWB Archives. Original Author: Ram Shankar Yadav

New on Geeks with Blogs