【问题标题】:Gridview Header with sort-icon带有排序图标的 Gridview 标题
【发布时间】:2025-12-28 15:15:06
【问题描述】:

我正在将数据集绑定到 VB .net 中的 GridView。我有一些自定义排序设置,如果选择了我的 3 个选项之一,我希望在标题旁边显示一个图标。

我已经阅读了很多这样做的方法,我看到 Gridviews 甚至有一个 ASC 和 DESC 标题样式我可以与视图相关联。不过,我有两个问题:

  1. 我正在排序触发器上使用 linq 对列表进行排序,然后将其绑定到数据网格。
  2. 我这样做的原因是我想保持多个排序级别,按 3 列而不是 1 列排序。

为清晰起见进行编辑 具体来说,我想要做的是遍历 GridView 的标题文本的值,看看它是否与我在视图状态中保存的内容相匹配,如果是,则特别为该标题添加图像。基本上如下所示,但是 headerRow.Cells(y).Text 总是返回“”,即使标题有文本:

Sub gvPatronData_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    Dim savedSortDirection(), savedSortColumn() As String
    Dim headerRow As GridViewRow = gvPatronData.HeaderRow

    'this sets the values of these variables 
    'as strings equal to the text displayed in the header of the gridview
    _patronBl.SplitPatronSort(savedSortDirection, SortDirection, savedSortColumn, SortColumn)

    If SortDirection <> "" Then
        If e.Row.RowType = DataControlRowType.Header Then
            For x = 0 To savedSortDirection.Length - 1
                For y = 0 To headerRow.Cells.Count - 1
                    If headerRow.Cells(y).Text = savedSortColumn(x) Then
                        If savedSortDirection(x) = "Ascending" Then
                            Dim bGStyle As New System.Web.UI.WebControls.Style()
                            bGStyle.CssClass = "upSort"
                            headerRow.Cells(y).ApplyStyle(bGStyle)
                        Else
                            Dim bGStyle As New System.Web.UI.WebControls.Style()
                            bGStyle.CssClass = "downSort"
                            headerRow.Cells(y).ApplyStyle(bGStyle)
                        End If

                    End If
                Next
            Next
        End If
    End If

End Sub

【问题讨论】:

标签: asp.net vb.net gridview


【解决方案1】:

您是否尝试循环 GridView 的 Columns 而不是 GridViewRow 的 Cell-Collection? DataControlField 有一个HeaderText 属性。

For Each col As DataControlField In gvPatronData.Columns
   If col.HeaderText = savedSortColumn(x) Then
      If savedSortDirection(x) = "Ascending" Then
         Dim bGStyle As New System.Web.UI.WebControls.Style()
         bGStyle.CssClass = "upSort"
         headerRow.Cells(gvPatronData.Columns.IndexOf(col)).ApplyStyle(bGStyle)
      Else
         Dim bGStyle As New System.Web.UI.WebControls.Style()
         bGStyle.CssClass = "downSort"
         headerRow.Cells(gvPatronData.Columns.IndexOf(col)).ApplyStyle(bGStyle)
      End If
   End If
Next

【讨论】:

  • 是的,这让我可以了解列标题文本,现在让我深入研究下一个问题!我无法让样式出现在页面上。谢谢蒂姆!
  • @Neil:你试过用 f.e. cell.CssClass = "upSort" 而不是 ApplyStyle?
  • 我会将结果包含在底部,以供将来查找的人参考。它现在正在工作
【解决方案2】:

这就是我最终做到的方式。实际上,钻入表格对象是我之前遇到的问题。

Sub gvPatronData_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim savedSortDirection(), savedSortColumn() As String
Dim columnCollection As DataControlFieldCollection = gvPatronData.Columns

_patronBl.SplitPatronSort(savedSortDirection, SortDirection, savedSortColumn, SortColumn)

If e.Row.RowType = DataControlRowType.Header Then
    For x = 0 To savedSortDirection.Length - 1
        For Each col As DataControlField In columnCollection
            If col.HeaderText = _headerDictionary(savedSortColumn(x)) Then
                If savedSortDirection(x) = "Ascending" Then
    e.Row.Cells(gvPatronData.Columns.IndexOf(col)).Attributes.Add("Style", _
      "background-image: url(images/arrow_up.png);background-repeat:no-repeat;background-position: 96% 50%;")
                Else
    e.Row.Cells(gvPatronData.Columns.IndexOf(col)).Attributes.Add("Style", _
      "background-image: url(images/arrow_down.png);background-repeat:no-repeat;background-position: 96% 50%;")
                End If
             End If
        Next
    Next
End If

_headerDictionary :这是我的 DataField 到 HeaderText 字符串的字典,因为我的 savedSortColumn 是要排序的数据字段。

定义:

Public Function ColumnDataFieldToHeaderTextDictionary() As Dictionary(Of String, String)
    Dim dict As New Dictionary(Of String, String)
    dict.Add("FirstName", "First Name")
    dict.Add("LastName", "Last Name")
    dict.Add("Phone", "Phone Number")
    dict.Add("Phone2", "Alternate Phone Number")
    dict.Add("Email", "Email")

    Return dict
End Function

【讨论】:

    最近更新 更多