Link to home
Start Free TrialLog in
Avatar of LenTompkins
LenTompkinsFlag for United States of America

asked on

C# changing backcolor on a cell in a datagridview

I am trying to change the color of a cell in a DataGridView based on the value of the column.  I have tried the following attempts:
 string value = this.dgvMainBottomRow1["colCostedBy", 0].Value.ToString();
 switch (value)
 {
   case "No Ticket Assigned":
     this.dgvMainBottomRow1["colCostedBy", 0].Style.BackColor = Color.Red;
   break;
 default:
   this.dgvMainBottomRow1["colCostedBy", 0].Style.BackColor = Color.White;
    break;
}
  I also tried:
 private void dgvMainBottomRow1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
 {
  // Check if this is the right column.
  if (e.ColumnIndex == 0)
  {  
    string value = e.Value.ToString();
    switch (value)
    {
      case "No Ticket Assigned":
         e.CellStyle.BackColor = Color.Red;
         break;
        default:
         e.CellStyle.BackColor = Color.White;
         break;
   }
}
When I step thru the code, it is setting the back color properly, but when I view the form, it is still not changed.

Is there a grid setting that needs to be changed?
ASKER CERTIFIED SOLUTION
Avatar of Anuradha Goli
Anuradha Goli
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LenTompkins

ASKER

Your sample helped me debug my problem.  The code works as I had coded it, but the column that needed the color changed was the first column of the row and the selection indicator was on that column and looked blue not red.  Once I moved the cursor to the next column the color came in as expected. Thanks