c# - 是否有一个我可以在我的类中使用的属性来告诉 DataGridView 在绑定(bind)到 List<MyClass> 时不要为其创建列

标签 c# winforms datagridview properties attributes

我有这样一个类:

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

当我创建 List<MyClass>并将其分配给 DataSourceDataGridView ,网格显示两列,“Foo/Bar”和“Baz”。这就是我想要发生的事情。

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

我也想使 Enabled 成为属性,但仍将其隐藏在 DataGridView 上。

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

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

最佳答案

[Browsable(false)]将从 DataGridView 中隐藏一个属性。

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.

关于c# - 是否有一个我可以在我的类中使用的属性来告诉 DataGridView 在绑定(bind)到 List<MyClass> 时不要为其创建列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1066907/

相关文章:

C#,BindingNavigator,右端丑陋的线条

c# - 在 C# 中添加字符串,编译器是如何做到的?

c# - 在 C# 中显示密码文本框的纯文本

c# - DataGridView/ComboBox 需要两种显示状态(长和短)的信息

c# - 类似于 WPF ItemsControl 的用户控件列表

c# - 在验证用于数字输入的文本框时,有没有办法避免抛出/捕获异常?

c# - 根据 C# 中的单元格值获取行数

c# - Xamarin.iOS NSUrlSession 不适用于 iOS 8

c# - TwoWay 集合绑定(bind)同步/锁定

c# - 不可变字符串误解或文档中的错误?