是否有我可以在我的类中使用的属性来告诉 DataGridView 在绑定到 List<MyClass> 时不要为其创建列? [英] Is there an Attribute I can use in my class to tell DataGridView not to create a column for it when bound to a List&lt;MyClass&gt;

查看:11
本文介绍了是否有我可以在我的类中使用的属性来告诉 DataGridView 在绑定到 List<MyClass> 时不要为其创建列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂这样的课:

private class MyClass {
  [DisplayName("Foo/Bar")]
  public string FooBar { get; private set; }
  public string Baz { get; private set; }      
  public bool Enabled;
}

当我创建一个 List 并将其分配给 DataGridViewDataSource 时,网格向我显示了两列,"Foo/Bar"和Baz".这就是我想要发生的事情.

When I create a List<MyClass> and assign it to the DataSource of a DataGridView, the grid shows me two columns, "Foo/Bar" and "Baz". This is what I want to happen.

它目前有效,因为 Enabled 是一个字段,而不是一个属性 - DataGridView 只会选取属性.然而,这是一个肮脏的黑客.

It currently works because Enabled is a field, not a property - DataGridView will only pick up properties. However, this is a dirty hack.

我也想让 Enabled 成为一个属性,但仍将其隐藏在 DataGridView 上.

I would like to make Enabled a property too, but still hide it on the DataGridView.

我知道我可以在绑定后手动删除列..但这并不理想.

I know I can manually delete the column after binding.. but that's not ideal.

是否有类似于 DisplayName 的属性,我可以用它来标记属性?类似于 [Visible(false)] ?

Is there an attribute similar to DisplayName, that I can tag a property with? Something like [Visible(false)] ?

推荐答案

[Browsable(false)] 将隐藏 DataGridView 的属性.

可视化设计器通常会在属性"窗口中显示那些没有可浏览属性或标有BrowsableAttribute 构造函数的可浏览参数设置为 true.这些成员可以在设计时修改.带有标记的成员BrowsableAttribute 构造函数的可浏览参数设置为 false 是不适合设计时编辑,因此不适合显示在视觉设计器中.默认为真.

A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true. These members can be modified at design time. Members marked with the BrowsableAttribute constructor's browsable parameter set to false are not appropriate for design-time editing and therefore are not displayed in a visual designer. The default is true.

这篇关于是否有我可以在我的类中使用的属性来告诉 DataGridView 在绑定到 List<MyClass> 时不要为其创建列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆