Results 1 to 5 of 5

Thread: GridView - inital load show sorting

  1. #1

    Thread Starter
    Fanatic Member snufse's Avatar
    Join Date
    Jul 2004
    Location
    Jupiter, FL
    Posts
    912

    GridView - inital load show sorting

    When I show a GridView for the first time I would like to show that the GridView is sorted on a specific column (Asc or Desc) (even if I have not yet clicked any sort columns). So what I do is using an "InitialLoad" variable = True and then I manually set the SortExpression and SortDirection. When user clicks on a sort column I set the "InitalLoad" = False. I don't know if there is a better way do do this?

  2. #2
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    830

    Re: GridView - inital load show sorting

    In your SQL statement if you are Displaying from it , add the Order by Clause and it will order it by your Column

  3. #3

    Thread Starter
    Fanatic Member snufse's Avatar
    Join Date
    Jul 2004
    Location
    Jupiter, FL
    Posts
    912

    Re: GridView - inital load show sorting

    Yes, I am doing so but I want to give the user a better indication on how the grid view is sorted initially.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: GridView - inital load show sorting

    Have a look at the sample code on this page:
    http://msdn.microsoft.com/en-us/libr...xpression.aspx

    It includes adding an image to indicate which direction the sort has occurred on. That is probably what you need.

    There's another sample method here:
    http://geekswithblogs.net/ram/archiv...sFunction.aspx

    That should also take care of your 'initial load' problem, because it will always show which direction the sort is occurring on.

  5. #5

    Thread Starter
    Fanatic Member snufse's Avatar
    Join Date
    Jul 2004
    Location
    Jupiter, FL
    Posts
    912

    Re: GridView - inital load show sorting

    When I initially load the GridView, I call the below:

    Code:
    Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowCreated
    
    If e.Row.RowType = DataControlRowType.Header Then
       Dim sortColumnIndex As Integer
       sortColumnIndex = GetSortColumnIndex()
    
       If ViewState("InitialSort1") = True Then
          sortColumnIndex = 0
          Session("SortDirection") = "ASC"
          ViewState("SortExpression") = "CompanyJobID"
          AddSortImage(sortColumnIndex, e.Row)
      Else
          If sortColumnIndex <> -1 Then
          Session("SortDirection") = GridViewSortDirection
          AddSortImage(sortColumnIndex, e.Row)  
      End If
    End If
    I have 4 columns in my grid view, column 0 = companyjobid (sortable), column 1 = date (sortable), column 2 = time (non sortable, colum 3 = Text (non sortable). At initial load the grid view is sorted (asc) through my sql statement and is being shown in this order. When I call the GetSortColumnIndex()" function, the value being returned is = 2 (I do not know why, maybe because no sortcolumn is found and column 2 is the firts non sortable column in my grid?). At the initial load the program does not know about any sorting (direction or expression) because no "sort click event" been raised. This is why I control sort expression and direction manually in my code.

    Now when user starts clicking on sortable columns everything is fine and works (correct column is sorted and correct image is applied (asc or desc).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width