I'm using TreeListView in windows application, I added some data to view, now I want to give color for a cell text based on some condition, please assist me to achieve this.
This is the sample code which I tried to display data.
var parent1 = new Node("PARENT1", "-", "-" );
parent1.Children.Add(new Node("CHILD_1_1", "A", "X"));
parent1.Children.Add(new Node("CHILD_1_2", "A", "Y"));
parent1.Children.Add(new Node("CHILD_1_3", "A", "Z"));
//here I need to give color for Third column value whose value is 'Y'
var parent2 = new Node("PARENT2", "-", "-" );
parent2.Children.Add(new Node("CHILD_2_1", "B", "W"));
parent2.Children.Add(new Node("CHILD_2_2", "B", "Z"));
parent2.Children.Add(new Node("CHILD_2_3", "B", "J"));
var parent3 = new Node("PARENT3", "-", "-");
parent3.Children.Add(new Node("CHILD_3_1", "C", "R"));
parent3.Children.Add(new Node("CHILD_3_2", "C", "T"));
data = new List<Node> { parent1, parent2, parent3 };
treeListView.Roots = data;
To change the formatting of an individual cell, you need to set UseCellFormatEvents to true and then listen for FormatCell events. To show just the credit balance in red, you could do something like this:
private void olv1_FormatCell(object sender, FormatCellEventArgs e) {
if (e.ColumnIndex == this.creditBalanceColumn.Index) {
Customer customer = (Customer)e.Model;
if (customer.Credit < 0)
e.SubItem.ForeColor = Color.Red;
}
}
@ Rev1.0 Thank you, it worked for me, but I used for e.ColumnIndex == 3(hardcoded) I am not getting this 'this.creditBalanceColumn.Index' , because column name is 'Credit ' can you please clarify this doubt.
creditBalanceColumn
is just an example for a column name. Just use the name of your column (should by default be something like olvColumn3 I think). You can view and change column names from the OLV column editor or from the designer generated code section. Using ColumnIndex is not the best idea because the index could change and break your code when you later add or remove columns.Thank you, Now I understood, previously I confused column title with OLV Column name.