How to give a color for a specific cell text in TreeListView

238 views Asked by At

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; 
1

There are 1 answers

3
Rev On BEST ANSWER

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;
    }
}

Source