alternate color gridview rows based on change of cell value asp.net

依然范特西╮ 提交于 2020-01-15 12:27:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!