Sort Indicator in Gridview header

Last post 04-21-2009 12:26 PM by shankel9760. 6 replies.

Sort Posts:

  • Sort Indicator in Gridview header

    04-18-2009, 7:14 AM
    • Member
      38 point Member
    • ashok.k
    • Member since 02-02-2009, 12:23 PM
    • Posts 117

    Hi

       I am sorting the data in the GridView when the user clicks the Column header.

    I want to display a SortIndicatior in the Column header.

    I want to show UP or DOWN arrow next to the text in the column header of the sorted column.

     How to display an image in the GridView Header next to the Header text?

    Thanks

    Ashok

  • Re: Sort Indicator in Gridview header

    04-18-2009, 7:40 AM
    Answer
    • Member
      141 point Member
    • pattanaik123
    • Member since 10-13-2007, 6:37 AM
    • India
    • Posts 212
    You can take a Image Field in your grid view header temple.On Sorting action you can access the header row,From that header row u can get the Image Field ID and as per your sorting direction,u can change the image.
    Debabrata Pattanaik
  • Re: Sort Indicator in Gridview header

    04-18-2009, 9:23 AM
    Answer
    • Contributor
      5,942 point Contributor
    • mo meng
    • Member since 08-30-2006, 3:56 AM
    • Singapore
    • Posts 1,227

    add the following css

    .sorticon
    {
     font-family: Webdings;
     font-size: x-small;
     color: #DDB86D;
    }

    in the gridview rowcreated, add the following control to the header

    Dim lblSorted As New Label

    lblSorted.CssClass = "sorticon"

    lblSorted.Text = IIf("change the code here to determine whether the column is currently in acs/desc" = "ASC", "5", "6").ToString

    e.Item.Cells(i).Controls.Add(lblSorted)

  • Re: Sort Indicator in Gridview header

    04-19-2009, 12:09 AM
    Answer
    • Contributor
      5,215 point Contributor
    • b471code3
    • Member since 02-27-2007, 5:44 PM
    • SE Minnesota
    • Posts 1,048

    I have a code snippet I saved sometime ago and it works like a charm.  Not sure why an image is not a property field in the .net 3.5 but hopefully they will add it later on.  Add this snippet in your code behind and just change the GridView's name as well as the location and name of the image you want to use.

     

        Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
            GridViewSortImages(sender, e)
        End Sub
    
        Sub GridViewSortImages(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            Dim senderGridView As GridView = CType(sender, GridView)
    
            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/1x1.gif"
                            If senderGridView.SortExpression = button.CommandArgument Then
                                If senderGridView.SortDirection = SortDirection.Ascending Then
                                    image.ImageUrl = "~/images/ascending.gif"
                                Else
                                    image.ImageUrl = "~/images/descending.gif"
                                End If
                            End If
                            cell.Controls.Add(image)
                        End If
                    End If
                Next
            End If
        End Sub
     
    View Brenden Kehren's LinkedIn profileView my profile
    Remember to mark as answer if this post answered your question or solved your problem.
  • Re: Sort Indicator in Gridview header

    04-19-2009, 1:55 AM
    Answer

     Set a CSS style based on the sort direction and supply a different CSS background-image with CSS background image positioning. That way, both the link text and the image are clickable.

    If you supply a link and a separate image, then the image must be a separate clickable image. You will need to supply the correct command args for the sort to work once the image is clicked.

    Please mark this post as the answer if you found it useful.

    Cheers

    Paul
  • Re: Sort Indicator in Gridview header

    04-19-2009, 4:57 AM
    Answer
    • Contributor
      2,466 point Contributor
    • anup1252000
    • Member since 11-12-2008, 8:26 AM
    • india
    • Posts 492

     protected void grdMovies_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.Header)
    {
    foreach (TableCell cell in e.Row.Cells)
    {
    LinkButton sortLink = (LinkButton)cell.Controls[0];
    if (sortLink.Text == grdMovies.SortExpression)
    {
    if (grdMovies.SortDirection == SortDirection.Ascending)
    sortLink.Text += “ <img src=’asc.gif’ title=
    ’Sort ascending’ />”;
    else
    sortLink.Text += “ <img src=’desc.gif’ title=
    ’Sort descending’ />”;
    }
    }
    }
    }

     

    this wil help u..

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Anup Hosur
    HP
    http://anup-anuphosur.blogspot.com/


  • Re: Sort Indicator in Gridview header

    04-21-2009, 12:26 PM
    • Member
      2 point Member
    • shankel9760
    • Member since 04-21-2009, 12:25 PM
    • Posts 1
    That worked absolutely perfect. Thanks.
Page 1 of 1 (7 items)