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

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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 63
Install Problem 13 34
.NET 2008 VB and C# 6 39
transaction in asp.net, sql server 6 43
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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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