我创建一个gridview并将其按第一列分组。我想对同一组中的所有行应用随机颜色。使用以下代码,所有gridview都有一种随机的背景色
'random color
Public Function color() As String
Dim random As New Random
Dim xromata As Drawing.Color = Drawing.Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255))
Dim xromolexi As String = ("#" & xromata.Name)
Return xromolexi
End Function
'group by first column
Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
For i As Integer = GridView1.Rows.Count - 1 To 1 Step -1
Dim row As GridViewRow = GridView1.Rows(i)
Dim previousRow As GridViewRow = GridView1.Rows(i - 1)
Dim omada As String = row.Cells(0).Text
Dim prevomada As String = previousRow.Cells(0).Text
If omada = prevomada Then
If previousRow.Cells(0).RowSpan = 0 Then
If row.Cells(0).RowSpan = 0 Then
previousRow.Cells(0).RowSpan += 2
Else
previousRow.Cells(0).RowSpan = row.Cells(0).RowSpan + 1
End If
row.Cells(0).Visible = False
End If
End If
row.Style.Add("background-color", color) 'random color
Next
End Sub
我想像这样显示