It can be useful at times to draw the users attention to a certain row or rows in a grid. You might want to point out an account that is about to expire or show that an entry requires approval. Normally, you would evaluate the data in the grid yourself to change the BackColor of a row. The RadGridView offers you the ability to create a condition that will apply certain formatting for you. I have created a simple class that will allow us to display a few laptop orders.
public class Laptop
{
public string Manufacturer { get; set; }
public double Price { get; set;}
public string CPU { get; set; }
public string Memory { get; set; }
public string OperatingSystem { get; set; }
public decimal OrderCount { get; set; }
}
Next I will setup my form with a RadGridView and add a few orders in the Form_Load event.
var laptops = new List<Laptop>
{
new Laptop()
{CPU = "1.6 GHz", Manufacturer = "HP", Memory = "3 GB", OperatingSystem = "Vista", Price = 899.99, OrderCount = 25},
new Laptop()
{CPU = "1.4 GHz", Manufacturer = "Apple", Memory = "3 GB", OperatingSystem = "OS10", Price = 899.99, OrderCount = 18},
new Laptop()
{CPU = "1.2 GHz", Manufacturer = "Joe Bob's Garage", Memory = "2 GB", OperatingSystem = "Linux", Price = 499.99, OrderCount = 3}
};
//bind data to grid
radGridView1.DataSource = laptops;
radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
We now see that our orders are being displayed.
We want to change any order with 25 items or more to have a red background and those with 3 items or less turn have a green background.
//create condition to let user know the order exceeds the maximum limit
var maxOrderCondition = new ConditionalFormattingObject("OrderCount", ConditionTypes.GreaterOrEqual, "25", "", true)
{
RowBackColor = Color.LightPink
};
radGridView1.Columns["OrderCount"].ConditionalFormattingObjectList.Add(maxOrderCondition);
//create condition to let user know the order is too small
var minOrderCondition = new ConditionalFormattingObject("OrderCount", ConditionTypes.LessOrEqual, "3", "", true)
{
RowBackColor = Color.LightGreen
};
radGridView1.Columns["OrderCount"].ConditionalFormattingObjectList.Add(minOrderCondition);
After some very simple code we now see that our rows highlighting is taken care of automatically on the bound data.