I assume you mean "based" on condition of column What specific
condition are you looking for. Are you looking to change the one
cell in a column, or the whole row based on a column setting, or did
you mean based on the condition of the row
There are a ton of events for changes to columns as well as rows in the
DataGridView class. Look through the list and you may find what
you need. The DataGridViewRow class provides a DefaultCellStyle
property that defines row-specific styles, and the DataGridView class
has an AlternatingRowsDefaultCellStyle property that applies to every
other row.
These and other DataGridView members are discussed in chapter 21 of my new book, if you're looking for a paper reference.
Ah, so you're looking to assign the row background based on the condition of a cell within that row.
Again, there are all sorts of events to track what changes when.
The most basic for this purpose might be the CellValueChanged event,
which occurs when a cell's value changes. For example, the following
code sets the background color to Red when the string value associated
with a cell is changed to "High"
private void myDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { int colIndex = e.ColumnIndex; int rowIndex = e.RowIndex;
privatevoid dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // If the column is the Artist column, check the // value. if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist") { if (e.Value != null) { // Check for the string "pink" in the cell. string stringValue = (string)e.Value; stringValue = stringValue.ToLower(); if(stringValue == "high")
change datagridview row color (winform 2.0)
DaveKoch
I have a requirement to color data grid row back color based on a given condition ( that is for a given column).
But when I run the code above it doesn't color grid cells. The event I am using is form's Load event or button click event.
Please help me to solve this issue.
Thanks
Supun
BjarneSCN
There are a ton of events for changes to columns as well as rows in the DataGridView class. Look through the list and you may find what you need. The DataGridViewRow class provides a DefaultCellStyle property that defines row-specific styles, and the DataGridView class has an AlternatingRowsDefaultCellStyle property that applies to every other row.
These and other DataGridView members are discussed in chapter 21 of my new book, if you're looking for a paper reference.
Erik
Joe Butler
Hi,
How can I change the color of the entire row based on a cell value.
Lets say Column "CustomerID" in a Rowcell has value 4 and this row should become red on DataGridView at fill time.
TNX
Laurel Hale
Again, there are all sorts of events to track what changes when. The most basic for this purpose might be the CellValueChanged event, which occurs when a cell's value changes. For example, the following code sets the background color to Red when the string value associated with a cell is changed to "High"
private void myDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
DataGridViewRow theRow = dataGridView1.Rows[rowIndex];
if (theRow.Cells[colIndex].Value.ToString() == "High")
theRow.DefaultCellStyle.BackColor = Color.Red;
}
}
Erik
Jetjez
is there a way to check the char instead of whole string
example. "High" , i want to check the letter H. if H exist then color this cell.
JDaniel
Just change (theRow.Cells[colIndex].Value.ToString() == "High" from above to
(theRow.Cells[colIndex].Value.Contains("H"))
Jon Limjap
Hai
kanlinkan