Search code examples
vb.netloopsgridviewrowdatabound

How to loop through specific columns in gridview?


I need to loop thru particualr columns in a gridview. For example, I need to loop every "%" columns and then assign colors according to their values, is there a simple way to achieve it? Thanks.

Gridview table enter image description here

RowDataBound event

    Protected Sub gv_ssi_rzli_data_1_RowDataBound(sender As Object, e As EventArgs) Handles gv_ssi_rzli_data_1.RowDataBound

    Dim gv As GridView = gv_ssi_rzli_data_1

    For i As Integer = 0 To gv.Rows.Count - 1

        If gv.Rows(i).Cells(0).Text = "Oct" Or
            gv.Rows(i).Cells(0).Text = "Nov" Or
            gv.Rows(i).Cells(0).Text = "Dec" Then

            If gv.Rows(i).Cells(2).Text >= Session("rzli_avg_blue_1") Then
                gv.Rows(i).Cells(2).BackColor = System.Drawing.Color.LightBlue
            ElseIf gv.Rows(i).Cells(2).Text >= Session("rzli_avg_green_1") And gv.Rows(i).Cells(2).Text < Session("rzli_avg_blue_1") Then
                gv.Rows(i).Cells(2).BackColor = System.Drawing.Color.LightGreen
            ElseIf gv.Rows(i).Cells(2).Text >= Session("rzli_avg_yellow_1") And gv.Rows(i).Cells(2).Text < Session("rzli_avg_green_1") Then
                gv.Rows(i).Cells(2).BackColor = System.Drawing.Color.LightYellow
            ElseIf gv.Rows(i).Cells(2).Text <= Session("rzli_avg_red_1") Then
                gv.Rows(i).Cells(2).BackColor = System.Drawing.Color.LightPink
            End If


        End If


    Next

End Sub

Solution

  • There are a number of ways to do this.

    First, I should probably point out that you seem to be reading through the entire grid every time you bind a row, which seems unnecessary.

    That aside, perhaps the simplest way would to be abstract the code that does the comparison to your value ranges into its own method, with the parameters to be the cell to be tested and modified. Since I assume from your use of the column index 2 that you know what columns are percentage columns, you would just call your new method with the relevant cell, like so:

    For i As Integer = 0 To gv.Rows.Count - 1
        Dim row = gv.Rows(i)
    
        Dim rowHeaderCell = row.Cells(0)
        if rowHeadercell.Text = "Oct" Or 
           rowHeaderCell.Text = "Nov" or 
           rowHeaderCell.Text = "Dec" Then
    
           SetCellBackGround(row.Cells(2))
           SetCellBackground(row.Cells(4))
           ' etc.
        End If
    Next
    

    I assume, above, that for whatever reason you want just to highlight Oct-Dec numbers. If you want that to vary, or want to use a different set of colors for other quarters, please say so.

    SetCellBackground would look like this (typed from memory):

    Sub SetCellBackground(TableCell cell) 
    
        If cell.Text >= Session("rzli_avg_blue_1") Then
                cell.BackColor = System.Drawing.Color.LightBlue
            ElseIf cell.Text >= Session("rzli_avg_green_1") And cell.Text < Session("rzli_avg_blue_1") Then
                cell.BackColor = System.Drawing.Color.LightGreen
            ElseIf cell.Text >= Session("rzli_avg_yellow_1") And cell < Session("rzli_avg_green_1") Then
                cell = System.Drawing.Color.LightYellow
            ElseIf cell <= Session("rzli_avg_red_1") Then
                cell.BackColor = System.Drawing.Color.LightPink
            End If
    
     End Sub