2011-09-19 53 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