Solved

C# changing backcolor on a cell in a datagridview

Posted on 2012-04-11
2
524 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now