GridView only sorting 1 direction

D

David C

I have a GridView in ASP.Net 2.0 that is bound to a list of files in a
specified directory. The GridView has AllowSorting="True" and I have
sorting expressions on 2 columns (Name and LastWriteTime). I would also
like to have the initial display of the GridView in DESC order by
LastWriteTime. When I click on a sortable column head, it sorts the first
time, but not the second (should toggle between ASC and DESC). I think it
may have to do with having to re-create the DataSource each time. I have a
Sub that is run from the top of the page as follows:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%

PopulateArticleGridView()

%>


Private Sub PopulateArticleGridView()
...code here to get directory name
Dim files As FileInfo() = New
DirectoryInfo(strPathPhy).GetFiles()

Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)

gvArticleList.DataSource = files
gvArticleList.DataBind()

End Sub


Private Function FileInfoComparison(ByVal fi1 As FileInfo, ByVal fi2 As
FileInfo) As Integer
Select Case txtSortOn.Text
Case "Name"
Return String.Compare(fi1.Name, fi2.Name)
Case Else
Return Date.Compare(fi1.LastWriteTime, fi2.LastWriteTime)
End Select
End Function

Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal
isPageIndexChanging As Boolean) As DataView
If Not dataTable Is Nothing Then
Dim dataView As New DataView(dataTable)
If GridViewSortExpression <> String.Empty Then
If isPageIndexChanging Then
dataView.Sort = String.Format("{0} {1}",
GridViewSortExpression, GridViewSortDirection)
Else
dataView.Sort = String.Format("{0} {1}",
GridViewSortExpression, GetSortDirection())
End If
End If
Return dataView
Else
Return New DataView()
End If
End Function

Private Property GridViewSortDirection() As String
Get
Return IIf(ViewState("SortDirection") = Nothing, "ASC",
ViewState("SortDirection"))
End Get
Set(ByVal value As String)
ViewState("SortDirection") = value
End Set
End Property

Private Property GridViewSortExpression() As String
Get
Return IIf(ViewState("SortExpression") = Nothing, String.Empty,
ViewState("SortExpression"))
End Get
Set(ByVal value As String)
ViewState("SortExpression") = value
End Set
End Property

Private Function GetSortDirection() As String
Select Case GridViewSortDirection
Case "ASC"
GridViewSortDirection = "DESC"
Case "DESC"
GridViewSortDirection = "ASC"
Case Else
GridViewSortDirection = "DESC"
End Select
Return GridViewSortDirection
End Function

Protected Sub gvArticleList_Sorting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewSortEventArgs)
txtSortOn.Text = e.SortExpression
GridViewSortExpression = e.SortExpression
'Dim pageIndex As Integer = gvArticleList.PageIndex
gvArticleList.DataSource = SortDataTable(gvArticleList.DataSource,
False)
gvArticleList.DataBind()
'gvArticleList.PageIndex = pageIndex
End Sub

Protected Sub gvArticleList_Sorted(ByVal sender As Object, ByVal e As
System.EventArgs)
' Display the sort expression and sort direction.
txtMsg.Text = "Sorting by " & _
gvArticleList.SortExpression.ToString() & _
" in " & gvArticleList.SortDirection.ToString() & _
" order."

End Sub

Any help is greatly appreciated.
-David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top