在没有任何数据绑定控件的情况下在 gridview 中排序 - 如何做到这一点?

发布于 2024-09-15 17:56:28 字数 217 浏览 8 评论 0原文

我的页面上有一个 GridView 控件。 GV不使用任何.net控件作为数据源来绑定数据。我在创建自己的数据表和数据集的代码后面执行此操作。然后我将此数据集绑定到 GV。 接下来我想启用对列进行排序。问题是,当您不使用任何预定义的数据源控件时,这将无法正常工作。你必须写一些代码。老兄我不知道什么代码,用户点击heder后如何对GV进行排序?然后应按该列中的数据对行进行排序。如何通过单击同一链接来启用升序和降序排序?

I have a GridView control on my page. GV doesn't use any .net control as a data source to bind to data. I'm doing this in code behind where I'm creating my own DataTable and DataSet. Then I'm binding this DataSet to GV.
Next i would like to enable of sorting colums. The issue is that this doesn't work as it should when you don't use any predefined data source control. You have to write some code. Bud I don't know what code, how to sort GV after user clicks heder? Then rows shoud be sorted by the data within this column. And how to enalbe ascending and descending sort by clicking the same link?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-09-22 17:56:29

这是我使用过的一些片段。当同一标题上的第 3 次单击删除该列上的排序并且还将排序顺序较低的列(比删除的排序表达式)提升到更高的排序顺序时,这很好,例如 此处此处。

  Protected Sub SortLinkBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)

     Dim gv As GridView = CType(sender.parent.parent.parent.parent, GridView)
     Call UpdateTabPanelProgressSpinner(gv)
     Dim dv As New DataView
     dv = gv.DataSource

     Call BuildSortExprTable("sort" + gv.ID, sender.commandname)
     dv.Sort = CurrSortExpressions("sort" + gv.ID)
     gv.DataSource = dv
     gv.DataBind()
  End Sub

Protected Function BuildSortExprTable(ByVal vsName As String, ByVal vsKey As String) As Dictionary(Of String, String)
  Dim SortTable As Dictionary(Of String, String)
  SortTable = IIf(ViewState(vsName) Is Nothing, New Dictionary(Of String, String), CType(ViewState(vsName), Dictionary(Of String, String)))
  If SortTable.Count = 0 Then
     SortTable.Add(vsKey, " ASC")
  Else

     If SortTable.ContainsKey(vsKey) Then
        Select Case SortTable(vsKey).ToString
           Case " ASC"
              SortTable(vsKey) = " DESC"
           Case " DESC"
              SortTable.Remove(vsKey)
        End Select
     Else
        SortTable.Add(vsKey, " ASC")
     End If
  End If

  ViewState.Add(vsName, SortTable)

  Return SortTable
  End Function

Protected Function CurrSortExpressions(ByVal vsName As String) As String
  Dim SortTable As Dictionary(Of String, String)

  SortTable = IIf(ViewState(vsName) Is Nothing, New Dictionary(Of String, String), CType(ViewState(vsName), Dictionary(Of String, String)))
  Dim sSorts As String = String.Empty
  Dim key As Object
  For Each key In SortTable.Keys
     sSorts += ", " + key.ToString() + " " + SortTable(key).ToString()
  Next
  'remove first ", "
  If sSorts = "" Then
     'nada
  Else
     sSorts = Right(sSorts, sSorts.Length - 2)
  End If
  Return sSorts
  End Function

Here are some snippets I've used. It's nice when the 3:rd click on the same header removes the sorting on that column and also promotes the columns of a lower sortorder, than the removed sortexpression, to a higher sortorder, such here and here.

  Protected Sub SortLinkBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)

     Dim gv As GridView = CType(sender.parent.parent.parent.parent, GridView)
     Call UpdateTabPanelProgressSpinner(gv)
     Dim dv As New DataView
     dv = gv.DataSource

     Call BuildSortExprTable("sort" + gv.ID, sender.commandname)
     dv.Sort = CurrSortExpressions("sort" + gv.ID)
     gv.DataSource = dv
     gv.DataBind()
  End Sub

Protected Function BuildSortExprTable(ByVal vsName As String, ByVal vsKey As String) As Dictionary(Of String, String)
  Dim SortTable As Dictionary(Of String, String)
  SortTable = IIf(ViewState(vsName) Is Nothing, New Dictionary(Of String, String), CType(ViewState(vsName), Dictionary(Of String, String)))
  If SortTable.Count = 0 Then
     SortTable.Add(vsKey, " ASC")
  Else

     If SortTable.ContainsKey(vsKey) Then
        Select Case SortTable(vsKey).ToString
           Case " ASC"
              SortTable(vsKey) = " DESC"
           Case " DESC"
              SortTable.Remove(vsKey)
        End Select
     Else
        SortTable.Add(vsKey, " ASC")
     End If
  End If

  ViewState.Add(vsName, SortTable)

  Return SortTable
  End Function

Protected Function CurrSortExpressions(ByVal vsName As String) As String
  Dim SortTable As Dictionary(Of String, String)

  SortTable = IIf(ViewState(vsName) Is Nothing, New Dictionary(Of String, String), CType(ViewState(vsName), Dictionary(Of String, String)))
  Dim sSorts As String = String.Empty
  Dim key As Object
  For Each key In SortTable.Keys
     sSorts += ", " + key.ToString() + " " + SortTable(key).ToString()
  Next
  'remove first ", "
  If sSorts = "" Then
     'nada
  Else
     sSorts = Right(sSorts, sSorts.Length - 2)
  End If
  Return sSorts
  End Function
北笙凉宸 2024-09-22 17:56:28

设置AllowSorting=True并处理排序事件。在排序事件中,您可以切换 排序方向 如果排序表达式相同(即您再次单击相同的列标题)。请参阅这篇文章以快速入门。

Set AllowSorting=True and handle Sorting event. In sorting event, you can toggle sort direction if the sort expression is the same (i.e. you are clicking on same column header again). See this article for quick start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文