Gridview comparer courant de ligne à la ligne précédente

Je voudrais formater un gridview basée sur la comparaison de la valeur de ligne actuelle (colonne 2), avec la rangée précédente valeur. Donc, si ils sont de la même couleur de fond serait la même, dire vert. Si elles ne sont pas la même que la couleur d'arrière-plan rouge. Par exemple:

Gridview values
Car 1 (bg color green)
Car 1 (bg color green)
Car 2 (bg color red)
Car 3 (bg color green)
Car 3 (bg color green)
Car 3 (bg color green)

Je n'ai pas été en mesure d'obtenir que cela fonctionne. Voici le code que j'ai trouvé.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

    If e.Row.RowType = DataControlRowType.DataRow Then

        If Not e.Row.DataItem Is Nothing Then

            'switch for first row 
            If e.Row.RowIndex = 1 Then
                Dim Gprev As GridViewRow = GridView1.Rows(e.Row.RowIndex - 1)
                If Gprev.Cells(1).Text = e.Row.Cells(1).Text Then
                    'e.Row.Cells(1).Text = ""
                    e.Row.BackColor = Drawing.Color.Red
                End If
            End If

            'now continue with the rest of the rows
            If e.Row.RowIndex > 1 Then

                'set reference to the row index and the current value
                Dim intC As Integer = e.Row.RowIndex
                Dim lookfor As String = e.Row.Cells(1).Text

                'now loop back through checking previous entries for matches 
                Do
                    Dim Gprev As GridViewRow = GridView1.Rows(intC - 1)

                    If Gprev.Cells(1).Text = e.Row.Cells(1).Text Then
                        'e.Row.Cells(1).Text = ""
                        e.Row.BackColor = Drawing.Color.Red
                    End If

                    intC = intC - 1
                Loop While intC > 0

            End If

        End If

    End If

End Sub
  • Si vous souhaitez activer la couleur de la ligne entre rouge/vert lorsque la voiture est différente de la précédente ligne?
  • Oui, c'est correct
InformationsquelleAutor Mike | 2012-03-09