|
-
May 16th, 2008, 02:23 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Not getting indication of gridview's sort column
I got this code off MSDN, and it's not working! (Am I the only one who's shocked?!)
When the user sorts a gridview column, I'd like to give him an indication of what column he has sorted on, in case he's an idiot, I guess (actually, my primary user says who cares but since I'm 3/4 of the way there maybe I'll do it anyway, unless none of you cares enough to help! It's what the ASP version of our app is currently doing.)
So here's the code:
Code:
' Source: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sortexpression(VS.80).aspx
Protected Sub gvDocList_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDocList.RowCreated
' Use the RowType property to determine whether the
' row being created is the header row.
If e.Row.RowType = DataControlRowType.Header Then
' Call the GetSortColumnIndex helper method to determine
' the index of the column being sorted.
Dim sortColumnIndex As Integer = GetSortColumnIndex()
If sortColumnIndex <> -1 Then
' Call the AddSortImage helper method to add
' a sort direction image to the appropriate
' column header.
AddSortImage(sortColumnIndex, e.Row)
End If
End If
End Sub
' This is a helper method used to determine the index of the
' column being sorted. If no column is being sorted, -1 is returned.
Function GetSortColumnIndex() As Integer
' Iterate through the Columns collection to determine the index
' of the column being sorted.
Dim field As DataControlField
For Each field In gvDocList.Columns
' md Added first condtion because it was saying I was sorting on my "Selected" checkbox column which
' isn't even sortable.
If field.SortExpression <> "" And field.SortExpression = gvDocList.SortExpression Then
Return gvDocList.Columns.IndexOf(field)
End If
Next
Return -1
End Function
' This is a helper method used to add a sort direction
' image to the header of the column being sorted.
Sub AddSortImage(ByVal columnIndex As Integer, ByVal row As GridViewRow)
' Create the sorting image based on the sort direction.
Dim sortImage As New Image()
If gvDocList.SortDirection = SortDirection.Ascending Then
sortImage.ImageUrl = "~/Images/UpArrow.gif"
sortImage.AlternateText = "Ascending Order"
Else
sortImage.ImageUrl = "~/Images/DownArrow.gif"
sortImage.AlternateText = "Descending Order"
End If
' Add the image to the appropriate header cell.
row.Cells(columnIndex).Controls.Add(sortImage)
End Sub
I added this one change:
' md Added first condtion because it was saying I was sorting on my "Selected" checkbox column which
' isn't even sortable.
If field.SortExpression <> "" And field.SortExpression = gvDocList.SortExpression Then
because it was going through the columns but always getting a match on the first one when my first column isn't even sortable.
Is this code no good?
In my ASP I have AllowSorting=True and on the columns I want to sort on I have SortExpression set (sorting is working fine, just not the extra touch the MS code was supposed to provide me).
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|