如何为 TreeListView 中的特定单元格文本指定颜色

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

我在 windows 应用程序中使用 TreeListView,我添加了一些要查看的数据,现在我想根据某些条件为单元格文本赋予颜色,请帮助我实现此目的。

这是我尝试显示数据的示例代码。

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

Source