C#: WinForms – How to change DataGridView cells color or other attributes
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);
}
}
}

(1,177)


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