温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - How to give a color for a specific cell text in TreeListView
.net c# objectlistview treelistview

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

发布于 2020-03-31 23:38:25

我在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; 

查看更多

提问者
G PADMAVATHI
被浏览
109
Rev1.0 2020-01-31 20:32

若要更改单个单元格的格式,需要将UseCellFormatEvents设置为true,然后侦听FormatCell事件。要仅以红色显示信用余额,您可以执行以下操作:

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

资源

最新帖子

最新文章