【发布时间】:2025-12-28 15:15:06
【问题描述】:
我正在将数据集绑定到 VB .net 中的 GridView。我有一些自定义排序设置,如果选择了我的 3 个选项之一,我希望在标题旁边显示一个图标。
我已经阅读了很多这样做的方法,我看到 Gridviews 甚至有一个 ASC 和 DESC 标题样式我可以与视图相关联。不过,我有两个问题:
- 我正在排序触发器上使用 linq 对列表进行排序,然后将其绑定到数据网格。
- 我这样做的原因是我想保持多个排序级别,按 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 的 GridView 而不是 Winform 的 DataGridView,不是吗?
-
您的实际问题是什么,您尝试过什么?这是一个简单的例子:codeproject.com/KB/aspnet/Gridview_Sorting_Paging.aspx