重写gridview,支持list和dataset自动排序,带排序箭头,顺便把自动翻页也加进去了.

    首先说明gridview是支持自动翻页和排序的(地球人都知道...), 但打多数开发情况数据源都是我们后台来绑定的,这样就不支持自动排序和翻页了。

    搜索到很多扩展gridview自动排序的,但到实际应用时发现有很多的不足,而且大多只支持dataset,要想支持list(of T)类型的泛类型的很少,自己写了个,记录一下。

 

  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Text
  5. Imports System.Web
  6. Imports System.Web.UI
  7. Imports System.Web.UI.WebControls
  8. Imports System.Data
  9. Imports System.Reflection
  10. Imports Logonexpress.DataService
  11. <ToolboxBitmap(GetType(GridView)), ToolboxData("<{0}:GridView runat=server></{0}:GridView>")> _
  12. Public Class GridView
  13.     Inherits WebControls.GridView
  14.     Public Delegate Sub BindHandler()
  15.     Private data As Object
  16.     Public Property DataSetSource() As Object    '定义数据源
  17.         Get
  18.             If data IsNot Nothing Then
  19.                 Return data
  20.             End If
  21.             Return Nothing
  22.         End Get
  23.         Set(ByVal value As Object)
  24.             Me.data = value
  25.         End Set
  26.     End Property
  27.     '排序字段
  28.     Protected Property SortExpressionStr() As String
  29.         Get
  30.             Return ViewState("SortExpression")
  31.         End Get
  32.         Set(ByVal value As String)
  33.             ViewState("SortExpression") = value
  34.         End Set
  35.     End Property
  36.     '获取排序表达式
  37.     Protected ReadOnly Property SortExpressionEx() As String
  38.         Get
  39.             If SortExpressionStr Is Nothing Then Return Nothing
  40.             Return Me.SortExpressionStr + " " + Me.SortDirectionStr
  41.         End Get
  42.     End Property
  43.     '获取排序方向
  44.     Protected Property SortDirectionStr() As String
  45.         Get
  46.             If ViewState("SortDirection"Is Nothing Then
  47.                 Return "DESC"
  48.             End If
  49.             If ViewState("SortDirection").ToString.ToLower = "asc" OrElse ViewState("SortDirection").ToString.ToLower = "desc" Then
  50.                 Return ViewState("SortDirection")
  51.             End If
  52.             Return "DESC"
  53.         End Get
  54.         Set(ByVal value As String)
  55.             ViewState("SortDirection") = value
  56.         End Set
  57.     End Property
  58.     '第一次翻页要还原排序方向,因为排序后我们就改变了排序方向,翻页时会重新绑定,所以要还原上一次的排序状态
  59.     Private Property IsPaging() As Boolean
  60.         Get
  61.             Return ViewState("IsPaging")
  62.         End Get
  63.         Set(ByVal value As Boolean)
  64.             ViewState("IsPaging") = value
  65.         End Set
  66.     End Property
  67.     <Category("Appearance")> _
  68.     <Editor("System.Web.UI.Design.ImageUrlEditor"GetType(System.Drawing.Design.UITypeEditor))> _
  69.     <Description("设置升序图标"), Browsable(True)> _
  70.     Public Property AscendingImageUrl() As String
  71.         Get
  72.             If ViewState("SortImageAsc") IsNot Nothing Then
  73.                 Return ViewState("SortImageAsc")
  74.             End If
  75.             Return ""
  76.         End Get
  77.         Set(ByVal value As String)
  78.             ViewState("SortImageAsc") = value
  79.         End Set
  80.     End Property
  81.     <Category("Appearance")> _
  82.     <Editor("System.Web.UI.Design.ImageUrlEditor"GetType(System.Drawing.Design.UITypeEditor))> _
  83.     <Description("设置降序图标"), Browsable(True)> _
  84.      Public Property DescendingImageUrl() As String
  85.         Get
  86.             If ViewState("SortImageDesc") IsNot Nothing Then
  87.                 Return ViewState("SortImageDesc")
  88.             End If
  89.             Return ""
  90.         End Get
  91.         Set(ByVal value As String)
  92.             ViewState("SortImageDesc") = value
  93.         End Set
  94.     End Property
  95.     '重写OnRowCreateed来设置排序图标
  96.     Protected Overrides Sub OnRowCreated(ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
  97.         If e.Row.RowType = DataControlRowType.Header Then
  98.             If Me.SortExpressionEx <> "" OrElse Me.SortExpressionEx <> String.Empty Then
  99.                 ShowSortImage(e.Row)
  100.             End If
  101.         End If
  102.     End Sub
  103.     Private Sub ShowSortImage(ByVal row As GridViewRow)
  104.         For Each tc As TableCell In row.Cells
  105.             If tc.Controls.Count > 0 And TypeOf tc.Controls(0) Is LinkButton Then
  106.                 Dim st As String = DirectCast(tc.Controls(0), LinkButton).CommandArgument
  107.                 If st = Me.SortExpressionStr Then
  108.                     Dim img As New System.Web.UI.WebControls.Image
  109.                     img.ImageUrl = IIf(Me.SortDirectionStr = "ASC"Me.AscendingImageUrl, Me.DescendingImageUrl)
  110.                     tc.Controls.Add(img)
  111.                     Exit Sub
  112.                 End If
  113.             End If
  114.         Next
  115.     End Sub
  116.     '重写排序方法
  117.     Protected Overrides Sub OnSorting(ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
  118.         Me.SortExpressionStr = e.SortExpression
  119.         IsPaging = True
  120.         RaiseEvent GetBindData()
  121.         If Me.SortDirectionStr.ToLower = "asc" Then
  122.             Me.SortDirectionStr = "DESC"
  123.         Else
  124.             Me.SortDirectionStr = "ASC"
  125.         End If
  126.     End Sub
  127.     '顺便重写一下翻页方法,之后只要在代码中允许翻页就可以了
  128.     Protected Overrides Sub OnPageIndexChanging(ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
  129.         If Me.IsPaging Then
  130.             SortDirectionStr = IIf(SortDirectionStr = "ASC""DESC""ASC")
  131.             IsPaging = False
  132.         End If
  133.         Me.PageIndex = e.NewPageIndex
  134.         RaiseEvent GetBindData()
  135.     End Sub
  136.     '获得数据源
  137.     Public Event GetBindData()
  138.     '重点:如果是List(of T)类型的数据源,就要把T类型传过来,在排序接口中使用
  139.     Public Sub OnBind(Of T)(Optional ByVal pageIndex As Integer = 0)
  140.         If Me.data IsNot Nothing Then
  141.             If SortExpressionStr = Nothing Then
  142.                 Me.DataSource = data
  143.                 Me.DataBind()
  144.                 Exit Sub
  145.             End If
  146.             Select Case data.GetType.ToString
  147.                 Case GetType(DataSet).ToString
  148.                     Dim dv As DataView = DirectCast(data, DataSet).Tables(0).DefaultView
  149.                     dv.Sort = Me.SortExpressionEx
  150.                 Case GetType(DataTable).ToString
  151.                     Dim dv As DataView = DirectCast(data, DataTable).DefaultView
  152.                     dv.Sort = Me.SortExpressionEx
  153.                 Case Else
  154.                     Dim perwr As New PredicateWrapper()
  155.                     perwr.PropertyName = Me.SortExpressionStr
  156.                     perwr.IsAsc = Me.SortDirectionStr.ToUpper = "ASC"
  157.                     DirectCast(data, List(Of T)).Sort(AddressOf perwr.Comparsion(Of T))
  158.             End Select
  159.             Me.DataSource = data
  160.             Me.PageIndex = pageIndex
  161.             Me.DataBind()
  162.         End If
  163.     End Sub
  164. End Class

List排序类

  1. Imports Logonexpress.DataService
  2. Public Class PredicateWrapper
  3.     Public PropertyName As String
  4.     Public IsAsc As Boolean
  5.     Public Sub New()
  6.     End Sub
  7.     Public Sub New(ByVal propertyname As StringByVal isAsc As Boolean)
  8.         propertyname = propertyname
  9.         isAsc = isAsc
  10.     End Sub
  11.     Public Function Comparsion(Of T)(ByVal x As T, ByVal y As T) As Integer
  12.         If x Is Nothing AndAlso y Is Nothing Then
  13.             Return 0
  14.         ElseIf x Is Nothing Then
  15.             Return -1
  16.         ElseIf y Is Nothing Then
  17.             Return 1
  18.         End If
  19.         Dim getter As MemberGetDelegate = MemberAccessor.Instance.GetterDelegate(GetType(T), PropertyName)
  20.         Dim vx As Object = getter(x)
  21.         Dim vy As Object = getter(y)
  22.         Dim result1 As Integer = comparer.Default.Compare(vx, vy)
  23.         If result1 <> 0 Then
  24.             If Not IsAsc Then
  25.                 Return Not result1
  26.             Else
  27.                 Return result1
  28.             End If
  29.         End If
  30.         Return 0
  31.     End Function
  32. End Class

 说明: 部分命名空间和类是引用公司的.

 

之后只要在代码中添加:Gridview就已经支持List和Dataset数据源的排序和分页了。

  1.     Protected Sub GridView1_GetBindData() Handles GridView1.GetBindData
  2.         Me.GridView1.DataSetSource = GetDb.GetDb
  3.         Me.GridView1.OnBind(Of DataTable)()
  4.     End Sub
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值