2012-03-09 116 views
2

我想根据比较当前行值(第2列)和前一行值来格式化gridview。所以如果他们是相同的背景颜色将是相同的,说绿色。如果它们不相同,背景颜色会变成红色。例如: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 2012-03-09 14:23:55

+0

是的,这是正确的 – Mike 2012-03-09 14:24:28

回答

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。 – Mike 2012-03-09 16:29:36

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 
相关问题