我有一个这样的课:
private class MyClass {
[DisplayName("Foo/Bar")]
public string FooBar { get; private set; }
public string Baz { get; private set; }
public bool Enabled;
}
Run Code Online (Sandbox Code Playgroud)
当我创建一个List<MyClass>并将其分配给DataSourcea时DataGridView,网格显示两列,"Foo/Bar"和"Baz".这就是我想要发生的事情.
它目前有效,因为Enabled是一个字段,而不是属性 - DataGridView只会获取属性.但是,这是一个肮脏的黑客.
我也想使Enabled成为一个属性,但仍然将它隐藏在DataGridView上.
我知道我可以在绑定后手动删除列..但这并不理想.
是否有类似于DisplayName的属性,我可以用?标记属性?有点像[Visible(false)]?
我在c#中有一个小应用程序,它有一个使用以下内容填充的DataGridView:
grid.DataSource = MyDatasource array;
MyClass保存列的结构,它看起来像这样:
class MyDatasource
{
private string column1;
private string column2;
public MyDatasource(string arg1, string arg2)
{
this.column1 = arg1;
this.column2 = arg2;
}
public string column1
{
get
{
return this.column1;
}
set
{
this.column1 = value;
}
}
public string column2
{
get
{
return this.column2;
}
set
{
this.column1 = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,DataGridView会填充正确的数据,但现在我想隐藏column2.我尝试[Browsable(false)]在列声明之上添加,这将隐藏它,但我还需要从代码中访问列值,当我使用[Browsable(false)]并尝试读取内容时,它就像列不存在一样.如果我不使用它,我可以毫无问题地阅读该列,但它在DataGridView中可见.
我怎么能隐藏列但仍能从代码中读取其内容?