DataGridView changing cell background color
I finally managed to get it working. Here the code :
private void dgvStatus_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex != color.Index)
return;
e.CellStyle.BackColor = Color.FromArgb(int.Parse(((DataRowView)dgvStatus.Rows[e.RowIndex].DataBoundItem).Row[4].ToString()));
}
if anyone know a better to do this please don't hesitate to post it. I'm open to suggestion
Changing datagridview cell color dynamically
This works for me
dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
Unable to change the background color of DataGridView cells
Try this, I guess this is what you are looking for..
for(int i = 0; i < dataGridView1.Rows.Count; i++)
{
int val = Int32.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
if (val < 5)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
Credits: here
Changing the cell background color of a 'DataGridView' in virtual mode
You need to handle the DataGridView.CellFormatting
event. For example:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 3 && e.Value == targetValue)
e.CellStyle.BackColor = Color.Red;
else
e.CellStyle.BackColor = SystemColors.Window;
}
Cheers
Changing Background Color of DataGridview Cells
You can catch the RowPostPaint event of you Data Grid View.
In VB.NEt it looks like :
Public Sub repainWithGrayColor() Handles DataGridView1.RowPostPaint
If (condition) Then
DataGridView1.Rows(e.RowIndex).Cells("ColumnName").Style.BackColor = Color.Gray
DataGridView1.Rows(e.RowIndex).Cells("ColumnName").ReadOnly = True
End If
End Sub
Hope it helped.
c# changing background color of cell in dataGridView if its value changed
The DataGridViewCellEventArgs argument contains all you need here;
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Version")
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.AliceBlue;
}
}
DataGridView change cells background and restore default style
This is one way to do it:
private void dgvLogHeader_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
foreach (DataGridViewColumn col in dgvLogHeader.Columns)
{
if (col.DefaultCellStyle.BackColor != Color.Empty)
col.DefaultCellStyle.BackColor = Color.Empty;
}
dgvLogHeader.Columns[e.ColumnIndex].DefaultCellStyle.BackColor = Color.Gainsboro;
}
This first resets each column with a color to default (Color.Empty
) and then colors the current column.
Note that this will not reset any colors you have set in individual cells!
The reset those you need to set each to Color.Empty
.
You may want to add a
dgvLogHeader.ClearSelection();
to clear the selection of the clicked cell.
But: If you need to decide on the colors on an individual basis, depending on Cell values then you will have to cycle over the Cells. This is best done in the CellPainting
event as this is called in an optimized way to include only the shown cells. Note that it is called on a per cell basis, so you need to honor the e.ColumnIndex
and e.RowIndex
values..
Update: Now tht you have clarfied to question, indeed you need to loop oner either all or all visible cells..
Here is a function you could call to do so:
private void markCells(DataGridView dgv, string pattern)
{
dgv.SuspendLayout();
foreach (DataGridViewRow row in dgv.Rows)
foreach (DataGridViewCell cell in row.Cells)
cell.Style.BackColor = cell.Value.ToString() == pattern ?
Color.LightBlue : Color.Empty;
dgv.ResumeLayout();
//dgv.ClearSelection()
}
If quickly goes over the whole DGV; you could call it e.g. from a Textbox.TextChange
event.
It suspends layout while setting the colors, so it should be fast and without flicker..
If you only want to search in one column you can add an extra condition, maybe like this:
cell.Style.BackColor = cell.Value.ToString() == pattern &&
cell.ColumnIndex == dgv.CurrentCell.ColumnIndex?
and also put it in a CellClick
event
Changing datagridview cell color based on condition
You need to do this
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow Myrow in dataGridView1.Rows)
{ //Here 2 cell is target value and 1 cell is Volume
if (Convert.ToInt32(Myrow .Cells[2].Value)<Convert.ToInt32(Myrow .Cells[1].Value))// Or your condition
{
Myrow .DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow .DefaultCellStyle.BackColor = Color.Green;
}
}
}
Meanwhile also take a look at Cell Formatting
Related Topics
Check If a Property Exists in a Class
Does Reactive Extensions Support Rolling Buffers
Getting the Index of a Particular Item in Array
Difference Between the Keydown Event, Keypress Event and Keyup Event in Visual Studio
Keep Casing When Serializing Dictionaries
How Do Events Cause Memory Leaks in C# and How Do Weak References Help Mitigate That
Can Someone Explain How Bcrypt Verifies a Hash
How to Use Datareceived Event of the Serialport Port Object in C#
When to Use Releasecomobject VS Finalreleasecomobject
Creating an Mvvm Friendly Dialog Strategy
How to Enable Nullable Reference Types Feature of C# 8.0 for the Whole Project
Linq: Passing Lambda Expression as Parameter to Be Executed and Returned by Method
Why Won't Anyone Accept Public Fields in C#
Project Builds But Can't Publish
Ssis Task for Inconsistent Column Count Import
How to Display the String HTML Contents into Webbrowser Control