开发者

Gridview Header with sort-icon

开发者 https://www.devze.com 2023-04-06 03:42 出处:网络
I am binding a data set to a GridView in VB .net.I have some custom sorting setup, and want to display an icon next to the header if one of my 3 options is selected.

I am binding a data set to a GridView in VB .net. I have some custom sorting setup, and want to display an icon next to the header if one of my 3 options is selected.

I've read a lot of methods in doing this, and I see Gridviews even have a ASC and DESC header style I can associate with the view. I have 2 problems with this, though:

  1. I am sorting a List with linq on Sort Trigger, then binding it to the datagrid.
  2. The reason I do it this way, is I want to maintain multiple sort levels, ordering by 3 columns rather than 1.

Edit for clarity Specifically what I want to do is loop through the value of a GridView's Header text, see if it matches what I have saved in viewstate, and if so add an image for that header in particular. Something essentially like below, however headerRow.Cells(y).Text is always returning "", even when the header has 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


Have you tried to loop the GridView's Columns instead of the the GridViewRow's Cell-Collection? A DataControlField has a HeaderText property.

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


This is how I ended up doing it. Actually drilling into the table objects was where I was coming up short before.

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 : This was a dictionary of my DataField to HeaderText strings, since my savedSortColumn was the data field to sort on.

Def:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消