Gridview compare current row to previous row

11,694

Solution 1

Code in c# but hopefully easy enough to translate...

In RowDataBound...

if (e.RowType != DataControlRowType.DataRow) return;  
if (e.Row.DataItemIndex ==0)  
{  
  e.Row.Cells[1].BackColor = Color.Green;  
  return;  
}  
var thisRow = e.Row;   
var prevRow = GridView.Rows[e.Row.DataItemIndex-1];  
e.Row.Cells[1].BackColor = (thisRow.Cells[1].Text == prevRow.Cells[1].Text) ? Color.Green : Color.Red;  
}

Note also green & red aren't generally good choices for contrast with each other (colour blindness)

Solution 2

Yet not tested but this should work or at least give you a hint:

Dim lastRowValue As String = ""

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
        e.Row.BackColor = If(e.Row.Cells(1).Text = lastRowValue AndAlso
                             e.Row.RowIndex <> 0,
                             Drawing.Color.Red,
                             Drawing.Color.Green)
        lastRowValue = e.Row.Cells(1).Text
    End If
End Sub

Note that you've used GridView1_RowDataBound but handled GridView1.RowCreated in your sample.

Share:
11,694
Mike
Author by

Mike

Updated on June 14, 2022

Comments

  • Mike
    Mike almost 2 years

    I would like to format a gridview based on comparing the current row value (column 2) with the previous row value. So if they are the same the background color would be that same, say green. If they are not the same the background color would be red. For example:

    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)
    

    I have not been able to get this to work. Here is the code I came up with.

    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