【问题标题】:What's the easiest way to rename the columns of a DataGridView showing a List<T>?重命名显示 List<T> 的 DataGridView 的列的最简单方法是什么?
【发布时间】:2010-10-10 22:07:39
【问题描述】:

我有一个 DataGridView,我将其 DataSource 设置为 List&lt;T&gt;。在这种情况下,T 是一个具有名为Foo 的属性的类,我想将其标题显示为Foo bar

如果是数据表,我可以更改查询:

select Foo as [Foo bar] from Baz

但是对于这样的事情,我将 DataGridView 的 DataSource 设置为 List&lt;Baz&gt;:

public class Baz {
   public string Foo { get; set; }
}

我无法将“Foo”重命名为“Foo bar”,因为它包含空格。是否必须手动重命名 DataGridViewColumn?

如果我可以使用类装饰器,最棒的是:

public class Baz {
   [DataGridViewColumnTitle("Foo bar")]
   public string Foo { get; set; }
}

但据我所知,标准库中不存在这样的东西。

我最好的选择是什么?

【问题讨论】:

    标签: c# .net list datagridview


    【解决方案1】:
    [DisplayName("Foo bar")]
    

    (在 System.ComponentModel 命名空间中;MSDN

    【讨论】:

    • 漂亮!我知道它必须存在:)
    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 2011-02-17
    • 1970-01-01
    相关资源
    最近更新 更多