November 24th, 2008, 06:39 AM
Coloring the cell of a GridView based on Condition [VS 2008 - ASP.NET/C#]
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:
(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.
November 25th, 2008, 12:06 AM
Use RowDataBound event of Gridview to accomplish this. You can change the color of cells by just changing it's CSS class if you want it CSS driven or you can write inline CSS for this.
Hope this helps.
November 25th, 2008, 12:17 AM
Use something like the following in the rowdatabound event of your gridview:
Originally Posted by Shaitan00
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "num2Column")) > 99)
//change your row background color
e.Row.BackColor = System.Drawing.Color.Gray;
//change specific row cell background colors
e.Row.Cells.BackColor = System.Drawing.Color.Red;
e.Row.Cells.ForeColor = System.Drawing.Color.Blue; ;