Come ordinare una GridView in ASP NET

settembre 6, 2009 by admin  
Filed under NET

 

Riporto qui il codice in Visual Basic NET per riordinare il controllo gridview in ASP NET.

Inserire un pulsante e fare riferimento a questa SUB che andrà a popolare una gridview e un datatable:

 

Protected dv As DataView

 

Private Sub loadGvElenco()

       Dim Classe as new Classe()
       Try
           Dim ds As DataSet
           ds = getDSSearch(Classe)
           dv = ds.Tables(0).DefaultView
           gvElenco.DataSource = dv
           dv.Sort = hidOrdinamento.Value
           gvElenco.DataBind()
       Catch ex As Exception
           Throw ex
       End Try
   End Sub

 

dove getDSSearch popola tramite un DB il Dataset DS. Poi inserire questo codice.

 

#Region "Ordinamento del GridView"
    Sub GridViewSortImages(ByRef sender As Object, ByRef e As GridViewRowEventArgs)
        Dim senderGridView As GridView = CType(sender, GridView)
        Dim space As New Literal
        space.Text = "    "

        If Not (e.Row Is Nothing) AndAlso e.Row.RowType = DataControlRowType.Header Then
            For Each cell As TableCell In e.Row.Cells
                If cell.HasControls Then
                    Dim button As LinkButton = CType((cell.Controls(0)), LinkButton)
                    If Not (button Is Nothing) Then
                        If GridViewSortExpression = button.CommandArgument Then
                            If GridViewSortDirection = "ASC" Then
                                button.Attributes("onmouseover") = "document.getElementById(’imgOrd" & cell.ClientID & "’).src=’../../images/sort_asc_h.gif’;"
                                button.Attributes("onmouseout") = "document.getElementById(’imgOrd" & cell.ClientID & "’).src=’../../images/sort_asc.gif’;"
                                button.Text = button.Text & "<img id=""imgOrd" & cell.ClientID & """ border=""0"" src=""../../images/sort_asc.gif"" />"
                            Else
                                button.Attributes("onmouseover") = "document.getElementById(’imgOrd" & cell.ClientID & "’).src=’../../images/sort_desc_h.gif’;"
                                button.Attributes("onmouseout") = "document.getElementById(’imgOrd" & cell.ClientID & "’).src=’../../images/sort_desc.gif’;"
                                button.Text = button.Text & "<img id=""imgOrd" & cell.ClientID & """ border=""0"" src=""../../images/sort_desc.gif"" />"
                            End If
                        Else
                            button.Attributes("onmouseover") = "document.getElementById(’imgOrd" & cell.ClientID & "’).src=’../../images/sort_none_h.gif’;"
                            button.Attributes("onmouseout") = "document.getElementById(’imgOrd" & cell.ClientID & "’).src=’../../images/sort_none.gif’;"
                            button.Text = button.Text & "<img id=""imgOrd" & cell.ClientID & """ border=""0"" src=""../../images/sort_none.gif"" />"
                        End If
                    End If
                End If
            Next
        End If
    End Sub

    Private Property GridViewSortDirection() As String
        Get
            Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))
        End Get

        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set
    End Property

    Private Property GridViewSortExpression() As String
        Get
            Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))
        End Get

        Set(ByVal value As String)
            ViewState("SortExpression") = value
        End Set
    End Property

    Private Function GetSortDirection() As String
        Select Case GridViewSortDirection
            Case "ASC"
                GridViewSortDirection = "DESC"
            Case "DESC"
                GridViewSortDirection = "ASC"
        End Select

        Return GridViewSortDirection
    End Function

    Protected Sub gvElenco_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvElenco.RowCreated
        Try
            GridViewSortImages(sender, e)
        Catch ex As Exception
            lblMessaggio.Text = ex.Message.ToString()
        End Try
    End Sub

    Protected Sub gvElenco_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvElenco.Sorting
        Try
            GridViewSortExpression = e.SortExpression
            Dim pageIndex As Integer = gvElenco.PageIndex
            SortDataTable(False)
        Catch ex As Exception
            lblMessaggio.Text = ex.Message.ToString()
        End Try
    End Sub

    Protected Sub SortDataTable(ByVal isPageIndexChanging As Boolean)
        Dim strSort As String = ""
        Try
            If GridViewSortExpression <> String.Empty Then
                If isPageIndexChanging Then
                    strSort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
                Else
                    strSort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
                End If
                hidOrdinamento.Value = strSort
                loadGvElenco()
            End If

        Catch ex As Exception
            lblMessaggio.Text = ex.Message.ToString()
        End Try
    End Sub
#End Region

Tags: , ,

Related posts