PDA

View Full Version : Coloring the cell of a GridView based on Condition [VS 2008 - ASP.NET/C#]


Shaitan00
11-24-2008, 11:40 AM
I am trying to find a way to display certain cells in RED or YELLOW based on specific criteria, ideally the cell itself could change color (it has to be very apparant) but in the worst-case a method of simply changing the color of the text would do - if there is no way to change the cell color.

Specifically, I have a DataSet with a table like the following:
[Date]-------[Type]--[Value]
11/20/2008---HEP-----10.1
11/20/2008---HAP-----7.8
11/21/2008---HEP-----12
11/21/2008---HAP-----100
11/21/2008---AAH-----2
(as an example)

Currently I get the DataSet (SQL table) and then set it as a datasource to my GridView on my ASP.NET web page - this works great.

Now I need to make the following changes:
- If [VALUE] is greater then 99 set the cell in question RED
- If [VALUE] is less then 10 set the cell in question YELLOW

I've got absolutly no clue how to accomplish this...
Is GridView the best method of displaying the data and setting colors? Is there an easy way to iterate over the data and determine which should be what color, is there a simpler approach?

Any help would be greatly appreciated.
Thanks,

SouthwaterDave
11-30-2008, 06:15 PM
A GridView is not the best control for this task. It is much easier to manipulate individual cells in a ListView (ASP.NET 3.5) or a DataList.

chattaman
12-06-2008, 06:29 PM
I am trying to find a way to display certain cells in RED or YELLOW based on specific criteria, ideally the cell itself could change color (it has to be very apparant) but in the worst-case a method of simply changing the color of the text would do - if there is no way to change the cell color.

Specifically, I have a DataSet with a table like the following:
[Date]-------[Type]--[Value]
11/20/2008---HEP-----10.1
11/20/2008---HAP-----7.8
11/21/2008---HEP-----12
11/21/2008---HAP-----100
11/21/2008---AAH-----2
(as an example)

Currently I get the DataSet (SQL table) and then set it as a datasource to my GridView on my ASP.NET web page - this works great.

Now I need to make the following changes:
- If [VALUE] is greater then 99 set the cell in question RED
- If [VALUE] is less then 10 set the cell in question YELLOW

I've got absolutly no clue how to accomplish this...
Is GridView the best method of displaying the data and setting colors? Is there an easy way to iterate over the data and determine which should be what color, is there a simpler approach?


You can use the GridView.RowDataBound event, details on link.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

and then have code within the event handler such as this, assuming that "Value" is the text in the column header of your GridView:

Dim myCell As TableCell = e.Row.Cells("Value")
If Not myCell Is Nothing Then
Try
Dim sngCellValue As Single = Single.Parse(myCell.Text)
If sngCellValue > 99 Then
myCell.BackColor = Drawing.Color.Red
ElseIf sngCellValue < 10 Then
myCell.BackColor = Drawing.Color.Yellow
End If
Catch ex As FormatException
End Try
End If

This should at least work for .NET Framework 2.0. Right off the bat, I don't see any reason why it wouldn't work for a later version.