Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Color Of Text in GridView Cell Without Changing Border Color

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.

enter image description here

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?

like image 224
Fred Avatar asked Oct 21 '22 09:10

Fred


1 Answers

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;
                }
}
like image 64
himak Avatar answered Oct 23 '22 11:10

himak