2012-03-09 7 views
2

Я хотел бы отформатировать gridview на основе сравнения текущего значения строки (столбец 2) с предыдущим значением строки. Поэтому, если они одинаковы, цвет фона будет таким же, скажем, зеленым. Если они не совпадают, цвет фона будет красным. Например:Gridview сравнить текущую строку с предыдущей строкой

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) 

Я не смог заставить это работать. Вот код, который я придумал.

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 
+0

Вы хотите переключить цвет строки между красным/зеленым цветом, когда автомобиль отличается от предыдущего? – Henry

+0

Да, это правильно – Mike

ответ

7

код в C#, но, надеюсь, достаточно легко перевести ...

В 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; 
} 

Отметим также зеленый & красный обычно не хороший выбор для контраста друг с другом (цвет слепота)

+0

Благодарим вас за помощь, вот вам vbcode f, кому это нужно. – Mike

2

Еще не проверял, но это должно работать или, по крайней мере, дать вам подсказку:

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 

Обратите внимание, что вы использовали в GridView1_RowDataBound но обрабатываются GridView1.RowCreated в вашей выборке.

0
    If e.Row.DataItemIndex = 0 Then 
       e.Row.BackColor = Drawing.Color.Blue 
      End If 

      Dim thisRow As GridViewRow = e.Row 

      If e.Row.RowIndex <> 0 Then 
       Dim prevRow As GridViewRow = GridView1.Rows(e.Row.DataItemIndex - 1) 

       If thisRow.Cells(1).Text = prevRow.Cells(1).Text Then 
        e.Row.BackColor = Drawing.Color.Blue 
        prevRow.BackColor = Drawing.Color.Blue 
       Else 
        e.Row.BackColor = Drawing.Color.DarkCyan 
       End If 

      End If 
Смежные вопросы