Fred
Fred

Reputation: 5808

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?

Upvotes: 5

Views: 8882

Answers (3)

Jun Yu
Jun Yu

Reputation: 424

I set the border color in css and it will not change the border like your code in almost all browsers except firefox.

.Gridview td
{
    border-right: lightsteelblue 1px solid;
    border-top: lightsteelblue 1px solid;
    border-left: lightsteelblue 1px solid;
    border-bottom: lightsteelblue 1px solid;
}

Upvotes: 0

himak
himak

Reputation: 144

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;
                }
}

Upvotes: 2

Blingers
Blingers

Reputation: 883

For anyone else that stumbles across this, you can get this to work by changing the forecolor and then changing all the borders back to black - probably not ideal but to change all the individual labels would be a nightmare. Code below (note you have to change each cell not just the row border) where grdSearchResults is your gridview:

 protected void grdSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {    
                if (e.Row.Cells[1].Text == "Closed")
                {
                    e.Row.ForeColor = System.Drawing.Color.LightGray;

                    for (int i = 0; i < grdSearchResults.Columns.Count; i++)
                    {
                        e.Row.Cells[i].BorderColor = System.Drawing.Color.Black;
                    }                 
                }    
            }
        }

Upvotes: 1

Related Questions