How can I make a DataGridView cell's font a particular color?

73,020

Solution 1

You can do this:

dataGridView1.SelectedCells[0].Style 
   = new DataGridViewCellStyle { ForeColor = Color.Yellow};

You can also set whatever style settings (font, for example) in that cell style constructor.

And if you want to set a particular column text color you could do:

dataGridView1.Columns[colName].DefaultCellStyle.ForeColor = Color.Yellow;
dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Blue;

updated

So if you want to color based on having a value in the cell, something like this will do the trick:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null && !string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
    {
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = new DataGridViewCellStyle { ForeColor = Color.Orange, BackColor = Color.Blue };
    }
    else
    {
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = dataGridView1.DefaultCellStyle;
    }
}

Solution 2

  1. To avoid performance issues (related to the amount of data in the DataGridView), use DataGridViewDefaultCellStyle and DataGridViewCellInheritedStyle. Reference: http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx

  2. You could use DataGridView.CellFormatting to paint effected Cells on previous code limitations.

  3. In this case, you need to overwrite the DataGridViewDefaultCellStyle, maybe.

//edit
In Reply to your comment on @itsmatt. If you want to populate a style to all rows/cells, you need something like this:

    // Set the selection background color for all the cells.
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

// Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default 
// value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty;

// Set the background color for all rows and for alternating rows.  
// The value for alternating rows overrides the value for all rows. 
dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray;
Share:
73,020
B. Clay Shannon-B. Crow Raven
Author by

B. Clay Shannon-B. Crow Raven

My novel about climate change and social justice featuring talking animals traveling through time and space to prevent disasters is now available on amazon, in three formats: Taterskin & The Eco Defenders Kindle eBook; Taterskin & The Eco Defenders Paperback; Taterskin & The Eco Defenders Hardcover Taterskin & The Eco Defenders, told in “first canine” by the titular character, a Labrador Retriever, is the story of a few humans and several talking animals who travel through time and space to make the past—and thus the future—a better place. The improvements effected by the Eco Defenders benefit not just the earth itself, but also mistreated humans and animals. In Book 1 (“Wonders Never Cease”), The Eco Defenders travel 150 million years into the past, to meet a Pterodactyl and make plans to “nip Nazism in the bud.” After that, it's on to 1787 Australia to protect the indigenous people and the environment there. The Eco Defenders next go to India, where they assemble animals from all over that country to put an end to Thuggee and fights to the death between Cobras and Mongooses. Their final stop is 1885 Africa, where the Eco Defenders band together with the local animals to prevent King Leopold of Belgium from taking control of the Congo, following which they put an end to the poaching of animals throughout the continent. Book 2 (“Tell it to Future Generations”) takes up with the Eco Defenders following up on their earlier adventures by 1) Preventing the American Civil War in 1861, after which a slave they free joins them; 2) Saving the Indians from being massacred at Wounded Knee in 1890, following which Chapawee, a Sioux Indian, joins the Eco Defenders; 3) Putting an end to the practice of vivisection (experimentation on live animals) in 1903; 4) Coming to the aid of exploited workers in 1911 Manhattan, saving hundreds from the Triangle Shirtwaist Fire; and 5) Traveling to the Amazon Basin in 1978 to protect and preserve the Amazon rainforest. @@@@@@@@@@@@@@@@@@@@@@@ I have lived in eight states; besides my native California (where I was born and where I now again reside), in chronological order I have infested: New York (Brooklyn), Montana (Helena), Alaska (Anchorage), Oklahoma (Bethany), Wisconsin (New Berlin and Oconomowoc), Idaho (Coeur d'Alene), and Missouri (Piedmont). I am a writer of both fiction (for which I use the nom de guerre "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006 and can be downloaded gratis here.

Updated on August 31, 2020

Comments

  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven almost 4 years

    This code works fine for making the cell's background Blue:

    DataGridViewRow dgvr = dataGridViewLifeSchedule.Rows[rowToPopulate];
    dgvr.Cells[colName].Style.BackColor = Color.Blue;
    dgvr.Cells[colName].Style.ForeColor = Color.Yellow;
    

    ...but the ForeColor's effects are not what I expected/hoped: the font color is still black, not yellow.

    How can I make the font color yellow?

    • KeithS
      KeithS almost 12 years
      Try also setting the SelectionBackColor and SelectionForeColor. If the row is selected, those colors will be used, not the ones for BackColor/ForeColor.
    • LarsTech
      LarsTech almost 12 years
      Can't reproduce the problem. My text is yellow on a blue background.
    • Jay Riggs
      Jay Riggs almost 12 years
      Any chance you've extended DataGridView and overidden OnPaintCell? If so this may help.
    • B. Clay Shannon-B. Crow Raven
      B. Clay Shannon-B. Crow Raven almost 12 years
      @Jay: No, I didn't extend and override.
  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven almost 12 years
    The following does nothing; in fact, when I do select a cell, it becomes white font on a blue blackground: dgvr.Cells[colName].Style.SelectionForeColor = Color.Blue; And I want all the cells to be yellow on blue, anyway, not just the selected one[s].
  • itsmatt
    itsmatt almost 12 years
    That's the selection color you're seeing, Clay. If you want the entire column to be yellow on blue, see my update.
  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven almost 12 years
    Sorry, I wasn't clear at first: I need neither the entire column nor just the selection color to be yellow on blue, but rather only/all cells that have a value. Empty cells should simply have a white backcolor/no special processing.
  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven almost 12 years
    This also (in the CellFormatting() event) works for the blue backcolor, but does nothing re: a yellow forecolor: if (e.Value != string.Empty) { e.CellStyle.BackColor = Color.Blue; e.CellStyle.ForeColor = Color.Yellow; }
  • itsmatt
    itsmatt almost 12 years
    see my update - you can handle that in the cellformatting handler.
  • varg
    varg almost 12 years
    i can confirm this solution. works perfect on my small testapplication.
  • NuWin
    NuWin almost 7 years
    @itsmatt is it possible to change the font color of a specific word/number in a column/row only rather than the entire column/row?