I have loaded the data from database to datagridview and have two columns target value and volume where volume >target value that volume cell should be in green color and
private void dataGridView1_DataBindingComplete(object sender DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToInt32(row.Cells["balaceAmount"].Value) == 0)
{
row.DefaultCellStyle.BackColor = Color.Yellow;
}
else
{
row.DefaultCellStyle.BackColor = Color.White;
}
}
}
private void dataMain_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataMain.Columns[e.ColumnIndex].Name == "colStatus")
{
if (int.Parse(e.Value.ToString()) == 2)
{
dataMain.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.OrangeRed;
dataMain.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
}
}
}
Let's say you have to color certain cell (not all cells of the row) by knowing two things:
In thas case you have to use event CellFormatting
In my case I use like this
private void DgvTrucksMaster_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow row in dgvTrucksMaster.Rows)
{
if (Convert.ToInt32(row.Cells["Decade1Hours"].Value) > 0)
{
row.Cells["Decade1Hours"].Style.BackColor = Color.LightGreen;
}
else if (Convert.ToInt32(row.Cells["Decade1Hours"].Value) < 0)
{
// row.DefaultCellStyle.BackColor = Color.LightSalmon; // Use it in order to colorize all cells of the row
row.Cells["Decade1Hours"].Style.BackColor = Color.LightSalmon;
}
}
}
And result you can see here
So here you can access certain cell of the row in column by its name row.Cells["Decade1Hours"]
How do you know this name? Well in my case i create column of DataGridView like this.
var Decade1Hours = new DataGridViewTextBoxColumn()
{
Name = "Decade1Hours",
Width = 50,
DataPropertyName = "Decade1Hours",
ReadOnly = true,
DefaultCellStyle = new DataGridViewCellStyle()
{
Alignment = DataGridViewContentAlignment.MiddleCenter,
ForeColor = System.Drawing.Color.Black,
Font = new Font(font, FontStyle.Bold),
Format = "n2"
},
HeaderCell = new DataGridViewColumnHeaderCell()
{
Style = new DataGridViewCellStyle()
{
Alignment = DataGridViewContentAlignment.MiddleCenter,
BackColor = System.Drawing.Color.Blue
}
}
};
Decade1Hours.HeaderText = "Дек.1";
dgvTrucksMaster.Columns.Add(Decade1Hours);
And well... you you need for instance colorize some of the cells in the row like ##1 4 5 and 8 you have to use cell index (it starts from 0).
And code will lok like
private void DgvTrucksMaster_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow row in dgvTrucksMaster.Rows)
{
if (Convert.ToInt32(row.Cells[1].Value) > 0 )
{
row.Cells[1].Style.BackColor = Color.LightGreen;
}
}
}
Surprised no one mentioned a simple if
statement can make sure your loop only gets executed once per format (on the first column, of the first row).
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// once per format
if (e.ColumnIndex == 0 && e.RowIndex == 0)
{
foreach (DataGridViewRow row in dgv.Rows)
if (row != null)
row.DefaultCellStyle.BackColor = Color.Red;
}
}
Kyle's and Simon's answers are gross waste of CPU resources. CellFormatting
and CellPainting
events occur far too many times and should not be used for applying styles. Here are two better ways of doing it:
If your DataGridView or at least the columns that decide cell style are read-only, you should change DefaultCellStyle of rows in RowsAdded
event. This event occurs only once when a new row is added. The condition should be evaluated at that time and DefaultCellStyle
of the row should be set therein. Note that this event occurs for DataBound situations too.
If your DataGridView or those columns allow editing, you should use CellEndEdit
or CommitEdit
events to change DefaultCellStyle
.
//After Done Binding DataGridView Data
foreach(DataGridViewRow DGVR in DGV_DETAILED_DEF.Rows)
{
if(DGVR.Index != -1)
{
if(DGVR.Cells[0].Value.ToString() == "البدلات")
{
CurrRType = "البدلات";
DataGridViewCellStyle CS = DGVR.DefaultCellStyle;
CS.BackColor = Color.FromArgb(0,175,100);
CS.ForeColor = Color.FromArgb(0,32,15);
CS.Font = new Font("Times New Roman",12,FontStyle.Bold);
CS.SelectionBackColor = Color.FromArgb(0,175,100);
CS.SelectionForeColor = Color.FromArgb(0,32,15);
DataGridViewCellStyle LCS = DGVR.Cells[DGVR.Cells.Count - 1].Style;
LCS.BackColor = Color.FromArgb(50,50,50);
LCS.SelectionBackColor = Color.FromArgb(50,50,50);
}
else if(DGVR.Cells[0].Value.ToString() == "الإستقطاعات")
{
CurrRType = "الإستقطاعات";
DataGridViewCellStyle CS = DGVR.DefaultCellStyle;
CS.BackColor = Color.FromArgb(175,0,50);
CS.ForeColor = Color.FromArgb(32,0,0);
CS.Font = new Font("Times New Roman",12,FontStyle.Bold);
CS.SelectionBackColor = Color.FromArgb(175,0,50);
CS.SelectionForeColor = Color.FromArgb(32,0,0);
DataGridViewCellStyle LCS = DGVR.Cells[DGVR.Cells.Count - 1].Style;
LCS.BackColor = Color.FromArgb(50,50,50);
LCS.SelectionBackColor = Color.FromArgb(50,50,50);
}
}
}