带有排序图标的Gridview标题

时间:2011-09-19 19:48:39

标签: asp.net vb.net gridview

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

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

  1. 我在排序触发器上使用linq对List进行排序,然后将其绑定到datagrid。
  2. 我这样做的原因是,我想保持多个排序级别,按3列而不是1列排序。
  3. 为了清晰起见而修改 具体来说,我想要做的是遍历GridView的Header文本的值,看看它是否与我在viewstate中保存的内容相匹配,如果是这样,特别是为该标题添加一个图像。有点像下面的东西,但 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
    

2 个答案:

答案 0 :(得分:1)

您是否尝试过循环GridView列而不是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

答案 1 :(得分:0)

这就是我最终这样做的方式。实际上钻进桌子的对象是我之前很快就要来的地方。

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