Ram Shankar Yadav's Blog!

My life's my passion!
posts - 170 , comments - 631 , trackbacks - 91

My Links

News


Microsoft Certified Professional Developer (MCPD) - .NET 2.0 Enterprise Application Development



Cheap Website Hosting - Free Domain

Twitter












Archives

Post Categories

Image Galleries

Blogs that I like most !

Sites that I like most !

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:


Use an Up and Down arrow in the GridView's header columns - by Fredrik Normen


Happy Coding :)

Print | posted on Thursday, July 13, 2006 2:48 PM | Filed Under [ Microsoft.NET ]

Feedback

Gravatar

# re: Generic Function for GridView Sort Images

good one Ram..
send me ur email, narendra.tiwari@india.rsystems.com is mine
7/20/2006 9:21 AM | Narendra Tiwari
Gravatar

# re: Generic Function for GridView Sort Images

AWESOME...EXACTLY WHAT I WAS LOOKING FOR
8/17/2006 1:54 AM | Beedo
Gravatar

# re: Generic Function for GridView Sort Images

I have something similar working for an SQLDataSource, however it seems to not work if you are using an ObjectDataSource.. anyone find a way around this? It seems that when you hit the RowCreated event the sortexpression for the gridview is empty...
8/22/2006 9:51 PM | Kristen
Gravatar

# re: Generic Function for GridView Sort Images

Great Function. Thanks a bunch.
10/20/2006 9:24 PM | Pal Joey
Gravatar

# re: Generic Function for GridView Sort Images

It works wonderfully. I binded the above function with the Row Created Event. Made some minor changes to make it accomodate my requirements. It works great.
3/30/2007 12:08 PM | Prashanth Gajra
Gravatar

# re: Generic Function for GridView Sort Images

good job! For perfomence reasons i would recommend retrieving the header row 1 time on a event the fires only once instead of the rowcreate event that fires for every row of the grid. After the sort is said and done this is what you can do on the Sorting event:

// Retrieve the header row.
GridViewRow headerRow = yourgrid.HeaderRow;

foreach (TableCell tableCell in headerRow.Cells)
{
// your code here (same as above or ...)
}
12/3/2007 11:41 PM | Ivatan Cavalcanti
Gravatar

# re: Generic Function for GridView Sort Images

Kristen post

when using custom objects with gridview you have to keep track yourself of the sortexpression and sortdirection. You can't rely on the gridview properties sortdirection and sortexpression to determine the column and image to insert. Instead of senderGridView.SortExpression == ... and senderGridView.SortDirection == ... you need to use the same information from your custom object. Hope this helps.
12/3/2007 11:49 PM | Ivatan Cavalcanti
Gravatar

# Sort Image

Thanks for the snippet! It saved me tons of time.
1/25/2008 12:29 AM | Patrick
Gravatar

# re: Generic Function for GridView Sort Images

It is an excellent peice of code. Thanks a ton.
10/23/2008 6:24 PM | Piyush
Gravatar

# re: Generic Function for GridView Sort Images

Wonderful piece of code.... great!!!
10/30/2008 2:05 PM | Sukanta Debnath
Gravatar

# re: Generic Function for GridView Sort Images

Good code !!!!
4/13/2009 6:41 PM | Nripin
Gravatar

# re: Generic Function for GridView Sort Images

bad code, not working properly
5/4/2010 4:39 PM | IDBI Intech
Gravatar

# re: Generic Function for GridView Sort Images

Hi,

This code works fine as long as we are using some data source like SqlDataSource. According to my findings senderGridView.SortExpression will always be empty. Check out this post for the fix.

Thank you
7/16/2010 5:30 PM | Awais
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: