Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# changing backcolor on a cell in a datagridview

Posted on 2012-04-11
2
Medium Priority
?
553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
Anuradha Goli earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

609 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