I am displaying data in a gridview and want to conditionally change the color of the text in a cell.
So on RowDataBound
of the grid
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((SurveyListHeader)(e.Row.DataItem)).PositionalConfidence != "G1" && ((SurveyListHeader)(e.Row.DataItem)).PositionalConfidence != "G3")
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
//e.Row.Cells[3].BorderColor = System.Drawing.Color.Black;
}
if (((SurveyListHeader)(e.Row.DataItem)).PositionalConfidence != "G2" && ((SurveyListHeader)(e.Row.DataItem)).PositionalConfidence != "G3")
{
e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
//e.Row.Cells[4].BorderColor = System.Drawing.Color.Black;
}
}
However this causes the border color to change too.
I have tried changing the border color back to black but this does not work.
I have tried adding a style item to the CSSStyleCollection of the cell, still no joy.
I have seen other people have had the problem but no answer works for me. Any suggestions?
I have used fill the border color to black before changing to red and then I have used else block to fill up the black color for the cells which are not changed to Red color...It worked perfectly. Here we can useany cell number. I used "3" for example.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text.StartsWith("-"))
{
// change the color
e.Row.Cells[3].BorderColor = System.Drawing.Color.Black;
e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
}
else
{
e.Row.Cells[3].BorderColor = System.Drawing.Color.Black;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With