Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > GridView Conditional Formatting
 

GridView Conditional Formatting

is it possible to do conditional formatting on the GridView data? Example: Making all values less than 0 have red text in one column. Thanks!
HMote  Wednesday, January 31, 2007 5:17 PM

Hi, HMote

Yes, it is. Presuming that "j" is an int according to which column you gonna to judge the value.

You can make a loop like the example:

(int i = 0; i < dataGridView1.Rows.Count; i++)

if (Convert.ToInt32(dataGridView1.Rows[ i ].Cells[ j ].Value) < 0)

Color.Blue;Color.Red;

//dataGridView1.Rows[ i ].Cells[ j ].Style -- whatever you want it to be...

Try this...

Code Block

(int i = 0; i < dataGridView1.Rows.Count; i++)

if (Convert.ToString(dataGridView1.Rows[ i ].Cells[<check cell>].Value)== "True")

dataGridView1.Rows[ i ].Cells[<change cell>].Style.ForeColor = Color.Red;

Hi, HMote

Yes, it is. Presuming that "j" is an int according to which column you gonna to judge the value.

You can make a loop like the example:

(int i = 0; i < dataGridView1.Rows.Count; i++)

if (Convert.ToInt32(dataGridView1.Rows[ i ].Cells[ j ].Value) < 0)

Color.Blue;Color.Red;

//dataGridView1.Rows[ i ].Cells[ j ].Style -- whatever you want it to be...

I loved this, it worked! Further question for my use.

I want to do the same thing, but I only want my cell to become red if Another cell on the same row is marked to "True". Can that be done, I've tried varying this code snippet, to no avail.

The Toddibear  Tuesday, October 16, 2007 6:14 PM

Try this...

Code Block

(int i = 0; i < dataGridView1.Rows.Count; i++)

if (Convert.ToString(dataGridView1.Rows[ i ].Cells[<check cell>].Value)== "True")

dataGridView1.Rows[ i ].Cells[<change cell>].Style.ForeColor = Color.Red;

You can use google to search for other answers

Custom Search

More Threads

• Datagridview not binding to certain datasets
• flexgrid again
• Is there any way to stop partial items in a listview?
• Is it possible to use DataGridViewRowCollection.List.RemoveRange?
• Binding data source to Checkedlistbox in C#.NET
• column alignment problem
• When to get data from the database
• Is there a way to stop the DataGridView from displaying a programmatically created detached new row?
• Databinding: Adding and Reading from Access DB Query
• ComboBox in DataGridView, setting the combo box's values accordingly to each row