问题
1 item
1 item
2 item
2 item
3 item
4 item
4 item
5 item
I have a gridview bound to a datasource. I want to change the color of the rows based on the GROUPING of the first column. I do not know what the values of the column will be, but basically I need to switch from one color to the other color when the value in that column changes. (I used bold to show what I mean) Once again I don't know the value in that column, so I can't just say "if it equals 1 then do this" - I know how to do that. :)) So just switch the row color from blue to red each time the value in column one changes. (i.e. red, red, blue, blue, red, blue, blue, red per the example above) Thanks for any suggestions. (I'm a VB person, but can manage to translate code if need be. Thanks again)
回答1:
I found the following (cept in C#, but here it is in VB) This works. :) I can setup the css for the two classes and works out fine. Every time the item in column 1 changes it changes the class on the row.
Dim currentClass As String = "alternateDataRow"
Dim currentGroup As String = ""
Protected Sub gvPayrollRecords_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvPayrollRecords.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rowGroup As String = e.Row.Cells(1).Text
If rowGroup <> currentGroup Then
If currentClass = "datarow" Then
currentClass = "alternateDataRow"
Else
currentClass = "datarow"
End If
currentGroup = rowGroup
End If
e.Row.CssClass = currentClass
End If
End Sub
来源:https://stackoverflow.com/questions/26288908/alternate-color-gridview-rows-based-on-change-of-cell-value-asp-net