2011-09-19 75 views
1

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

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

  1. 我排序列表与linq排序触发器,然后绑定到数据网格。
  2. 我之所以做这种方式,就是我要维护多个排序级别,由3列排序,而不是1

编辑为清楚起见 具体是什么我想要做的是遍历GridView的Header文本的值,看看它是否与我在viewstate中保存的内容相匹配,如果是,则特别为该头添加图像。本质的东西像下面,但headerRow.Cells(Y)。文本总是返回“”,即使头有文字:

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 
+0

我想弄清楚,你所谈论的ASP.NET的GridView和不WinForm的的DataGridView的,不是吗? –

+0

是的,ASP .NET的GridView。 –

+0

你的实际问题是什么,你试过甚么?下面是一个简单的例子:http://www.codeproject.com/KB/aspnet/Gridview_Sorting_Paging.aspx –

回答

1

您是否尝试过循环GridView的列,而不是在GridViewRow的Cell -采集? 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 
+0

是的,这让我可以看到列标题文本,并让我深入了解下一个问题!我无法让样式出现在页面上。谢谢蒂姆! –

+0

@尼尔:你有没有尝试过使用f.e. 'cell.CssClass =“upSort”'而不是'ApplyStyle'? –

+0

我会将结果包含在底部,以供将来参考。现在正在工作 –

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:这是我的数据字段来的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