|
-
May 26th, 2004, 10:35 AM
#1
Thread Starter
Fanatic Member
sorting a datagrid Ascending, descending
I want to sort datagrid columns ascending or descending. They always sort ascending.
How can I do descending.
This is the code I have in the Sort Command event.
VB Code:
Private Sub dgrResults_SortCommand1(ByVal source As Object, ByVal e As _
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgrResults.SortCommand
Dim dstStudents As DataSet = PopulateGrid()
' Create a DataView from the Dataset.
Dim dvwStudents As New DataView(dstStudents.Tables(0))
Try
' The DataView provides an easy way to sort. Simply set the
' Sort property with the name of the field to sort by.
dvwStudents.Sort = e.SortExpression
' Rebind the data source and specify that it should be sorted
' by the field specified in the SortExpression property.
dgrResults.DataSource = dvwStudents
dgrResults.DataBind()
Catch ex As Exception
Response.Redirect(clsLib.RedirectTo(ex.Message, "Search.aspx"))
End Try
End Sub
The PopulateGrid function returns a dataset of data.
-
May 26th, 2004, 11:11 AM
#2
just add DESC after the sortexpression ie.
VB Code:
dvwStudents.Sort = e.SortExpression & " DESC"
I think that should work anyway.
Here's an example from a datagrid that allows you to click a column heading to sort by that column. Clicking on that column heading again then sorts in the other direction.
VB Code:
Sub Sort_Grid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
If Session("SortColumn") = e.SortExpression.ToString() Then
'Same sort expression used as last time so reverse the order
If Session("SortOrder") = "" Then
Session("SortOrder") = " DESC"
Else
Session("SortOrder") = ""
End If
End If
Session("SortColumn") = e.SortExpression.ToString()
Session("SortExpression") = e.SortExpression.ToString() + Session("SortOrder")
gridLeaderBoard.DataSource = createDataSource()
gridLeaderBoard.DataBind()
End Sub
then in the createDataSource function...
VB Code:
dv.Sort = Session("SortExpression")
-
May 27th, 2004, 02:34 AM
#3
Thread Starter
Fanatic Member
Thanks again
FishCake helped me out with a similar sorting when paging problem here
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
|