摘要:[ASP.NET] GridView 排序符號
有時候有些客戶會要求在GridView上面有排序的功能,這對GridView來說實在太容易了,
只要GridView的AllowSorting="True"就可以使用這個功能了,
但是客戶常常會說可是都沒有符號,我怎麼知道他是怎麼排,
可是偏偏微軟就是沒提供這個功能,就只好自己來了,
我把他寫成一個簡單的副程式
Public Sub GridViewSorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
'此程式為增加升降冪符號在GridView中,GridView的事件狀態為Sorting
If ViewState("SortDir") Is Nothing Then
ViewState("SortDir") = SortDirection.Descending
End If
'把所有的符號清掉
For i As Integer = 0 To DirectCast(sender, GridView).Columns.Count - 1
DirectCast(sender, GridView).Columns(i).HeaderText = DirectCast(sender, GridView).Columns(i).HeaderText.Replace("▲", "")
DirectCast(sender, GridView).Columns(i).HeaderText = DirectCast(sender, GridView).Columns(i).HeaderText.Replace("▼", "")
Next
Dim ViewState_SortDirection As String = ViewState("SortDir").ToString() '定義GridView的升降冪
Dim Columns_i As Integer = 0
For i As Integer = 0 To DirectCast(sender, GridView).Columns.Count - 1
If e.SortExpression = DirectCast(sender, GridView).Columns(i).SortExpression Then '找出現在的Columns
Columns_i = i
If ViewState("SortDir").ToString() = SortDirection.Ascending.ToString() Then '若為升冪
e.SortDirection = SortDirection.Descending
DirectCast(sender, GridView).Columns(i).HeaderText = DirectCast(sender, GridView).Columns(i).HeaderText & "▼"
ViewState("SortDir") = SortDirection.Descending
Else '若其他則為降冪
e.SortDirection = SortDirection.Ascending
DirectCast(sender, GridView).Columns(i).HeaderText = DirectCast(sender, GridView).Columns(i).HeaderText & "▲"
ViewState("SortDir") = SortDirection.Ascending
End If
End If
Next
End Sub
'此程式為增加升降冪符號在GridView中,GridView的事件狀態為Sorting
If ViewState("SortDir") Is Nothing Then
ViewState("SortDir") = SortDirection.Descending
End If
'把所有的符號清掉
For i As Integer = 0 To DirectCast(sender, GridView).Columns.Count - 1
DirectCast(sender, GridView).Columns(i).HeaderText = DirectCast(sender, GridView).Columns(i).HeaderText.Replace("▲", "")
DirectCast(sender, GridView).Columns(i).HeaderText = DirectCast(sender, GridView).Columns(i).HeaderText.Replace("▼", "")
Next
Dim ViewState_SortDirection As String = ViewState("SortDir").ToString() '定義GridView的升降冪
Dim Columns_i As Integer = 0
For i As Integer = 0 To DirectCast(sender, GridView).Columns.Count - 1
If e.SortExpression = DirectCast(sender, GridView).Columns(i).SortExpression Then '找出現在的Columns
Columns_i = i
If ViewState("SortDir").ToString() = SortDirection.Ascending.ToString() Then '若為升冪
e.SortDirection = SortDirection.Descending
DirectCast(sender, GridView).Columns(i).HeaderText = DirectCast(sender, GridView).Columns(i).HeaderText & "▼"
ViewState("SortDir") = SortDirection.Descending
Else '若其他則為降冪
e.SortDirection = SortDirection.Ascending
DirectCast(sender, GridView).Columns(i).HeaderText = DirectCast(sender, GridView).Columns(i).HeaderText & "▲"
ViewState("SortDir") = SortDirection.Ascending
End If
End If
Next
End Sub
然後呼叫在GridView.Sorting的事件中參考你所要做排序的GridView就可以了
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles GridView1.Sorting
Me.GridViewSorting(Me.GridView1, e)
End Sub
Me.GridViewSorting(Me.GridView1, e)
End Sub