C# changing backcolor on a cell in a datagridview

Posted on 2012-04-11
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;
   this.dgvMainBottomRow1["colCostedBy", 0].Style.BackColor = Color.White;
  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;
         e.CellStyle.BackColor = Color.White;
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?
Question by:LenTompkins
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
LVL 12

Accepted Solution

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

Author Closing Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 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