Выделение цветом ячеек GridView

Зачастую при отображении GridView требуется подсветить отдельные строки, либо ячейки таблицы в зависимости от содержимого каждой ячейки.

На примере таблицы замера температуры тела сотрудников можно это осуществить несложным кодом в подпрограмме обработчика события:

Protected Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

            If MID(e.Row.Cells(2).Text,2,1) = "4" Then
		e.Row.Cells(2).BackColor = System.Drawing.ColorTranslator.FromHtml("#9d9c9b")
	    End If	 
            If MID(e.Row.Cells(2).Text,2,1) = "5" Then
		e.Row.Cells(2).BackColor = System.Drawing.ColorTranslator.FromHtml("#a9fcf9")
	    End If	 
            If MID(e.Row.Cells(2).Text,2,1) = "6" Then
		e.Row.Cells(2).BackColor = System.Drawing.ColorTranslator.FromHtml("#96faca")
	    End If	 
            If MID(e.Row.Cells(2).Text,2,1) = "7" Then
		e.Row.Cells(2).BackColor = System.Drawing.ColorTranslator.FromHtml("#f6dc8d")
	    End If	 
            If MID(e.Row.Cells(2).Text,2,3) = "7.2" OR MID(e.Row.Cells(2).Text,2,3) = "7.3"  OR MID(e.Row.Cells(2).Text,2,3) = "7.4" OR MID(e.Row.Cells(2).Text,2,3) = "7.5" OR MID(e.Row.Cells(2).Text,2,3) = "7.6" OR MID(e.Row.Cells(2).Text,2,3) = "7.7" OR MID(e.Row.Cells(2).Text,2,3) = "7.8" OR MID(e.Row.Cells(2).Text,2,3) = "7.9" Then 
		e.Row.Cells(2).BackColor = System.Drawing.ColorTranslator.FromHtml("#f4856b")
	    End If	 
            If MID(e.Row.Cells(2).Text,2,1) = "8" Then
		e.Row.Cells(2).BackColor = System.Drawing.ColorTranslator.FromHtml("#f90c38")
	    End If	 


            If MID(e.Row.Cells(3).Text,2,1) = "4" Then
		e.Row.Cells(3).BackColor = System.Drawing.ColorTranslator.FromHtml("#9d9c9b")
	    End If	 
            If MID(e.Row.Cells(3).Text,2,1) = "5" Then
		e.Row.Cells(3).BackColor = System.Drawing.ColorTranslator.FromHtml("#a9fcf9")
	    End If	 
            If MID(e.Row.Cells(3).Text,2,1) = "6" Then
		e.Row.Cells(3).BackColor = System.Drawing.ColorTranslator.FromHtml("#96faca")
	    End If	 
            If MID(e.Row.Cells(3).Text,2,1) = "7" Then
		e.Row.Cells(3).BackColor = System.Drawing.ColorTranslator.FromHtml("#f6dc8d")
	    End If	 
            If MID(e.Row.Cells(3).Text,2,3) = "7.2" OR MID(e.Row.Cells(3).Text,2,3) = "7.3"  OR MID(e.Row.Cells(3).Text,2,3) = "7.4" OR MID(e.Row.Cells(3).Text,2,3) = "7.5" OR MID(e.Row.Cells(3).Text,2,3) = "7.6" OR MID(e.Row.Cells(3).Text,2,3) = "7.7" OR MID(e.Row.Cells(3).Text,2,3) = "7.8" OR MID(e.Row.Cells(3).Text,2,3) = "7.9" Then
		e.Row.Cells(3).BackColor = System.Drawing.ColorTranslator.FromHtml("#f4856b")
	    End If	 
            If MID(e.Row.Cells(3).Text,2,1) = "8" Then
		e.Row.Cells(3).BackColor = System.Drawing.ColorTranslator.FromHtml("#f90c38")
	    End If	 

End Sub

В коде анализируется соответствующие строчные символы каждой ячейки и в зависимости от их значения определяется цвет самой ячейки.

Чтобы указанный выше код выполнялся, в самом GridView необходимо прописать обработчик события:

onrowdatabound="GridView_RowDataBound"

 

Комментарии закрыты