Solved

C# changing backcolor on a cell in a datagridview

Posted on 2012-04-11
2
531 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:LenTompkins
2 Comments
 
LVL 12

Accepted Solution

by:
Anuradha Goli earned 500 total points
ID: 37836771
Hi You can use code below to set style to cell
   private void dgvMainBottomRow1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewCellStyle styl = new DataGridViewCellStyle();
            styl.Alignment = DataGridViewContentAlignment.MiddleCenter;
            styl.BackColor = Color.Maroon;
            styl.ForeColor = Color.White;
            dgvMainBottomRow1.DefaultCellStyle = styl;
        }
Sample Image
0
 

Author Closing Comment

by:LenTompkins
ID: 37838537
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
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question