Home > WinForms > C#: WinForms – How to change DataGridView cells color or other attributes

C#: WinForms – How to change DataGridView cells color or other attributes

16:30

Marek Śliwiński Leave a comment Print This Post  (1,177) Go to comments

We can use for this _CellFormatting event. See below example:

private void DataGridView1_CellFormatting(object sender,
                        DataGridViewCellFormattingEventArgs e)
{
    DataGridView gv = (DataGridView)sender;

    if (gv.Columns[e.ColumnIndex].Name == "ColumnName")
    {
        // If cell has a value and contains char '-'
        if (e.Value != null && e.Value.ToString().Contains("-"))
        { // Change cell text color to red
            e.CellStyle.ForeColor = Color.Red;
            //e.CellStyle.Font = new Font("Arial", 10, FontStyle.Bold);
        }
    }
}
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: WinForms Tags: ,
  1. January 21st, 2010   (Quote) at 19:40   (Quote) | #1

    Thanks…Nice Post. Worked great! Could use a post on sorting the datagridview by decimal instead of strings. :)

  1. No trackbacks yet.

Subscribe without commenting