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

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.