C# 是否有我可以在我的类中使用的属性来告诉 DataGridView 在绑定到 List<MyClass> 时不要为其创建列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1066907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
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<MyClass>
提问by Blorgbeard is out
I have a class like this:
我有一个这样的课程:
private class MyClass {
[DisplayName("Foo/Bar")]
public string FooBar { get; private set; }
public string Baz { get; private set; }
public bool Enabled;
}
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.
当我创建 aList<MyClass>
并将其分配给DataSource
a 时DataGridView
,网格向我显示了两列,“Foo/Bar”和“Baz”。这就是我想要发生的事情。
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.
我也想让 Enabled 成为一个属性,但仍将其隐藏在 DataGridView 上。
I know I can manually delete the column after binding.. but that's not ideal.
我知道我可以在绑定后手动删除列.. 但这并不理想。
Is there an attribute similar to DisplayName, that I can tag a property with? Something like [Visible(false)]
?
是否有类似于 DisplayName 的属性,我可以用它来标记属性?像[Visible(false)]
什么?
采纳答案by C-Pound Guru
[Browsable(false)]
will hide a property from a DataGridView
.
[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.
可视化设计器通常会在“属性”窗口中显示那些没有可浏览属性或被标记为 BrowsableAttribute 构造函数的可浏览参数设置为 true 的成员。这些成员可以在设计时修改。标记为 BrowsableAttribute 构造函数的可浏览参数设置为 false 的成员不适合设计时编辑,因此不会显示在可视设计器中。默认值为真。
回答by Yoopergeek
I've been blissfully unaware that the System.ComponentModel
decorator attributes like BrowsableAttribute
and it's kin were related to anything other than binding to a PropertyGrid
. (facepalm) I like C-Pound Guru's approach because it allows you to keep your GUI more loosely coupled than what I've done in the past.
我一直很幸福地不知道System.ComponentModel
装饰器属性 likeBrowsableAttribute
和它的亲属除了绑定到PropertyGrid
. ( facepalm) 我喜欢 C-Pound Guru 的方法,因为它可以让你的 GUI 比我过去所做的更松散耦合。
Just for a different perspective, the approach I've used for a long time is to pre-define columns in your DataGridView, either programatically or through the Form Designer. When you do this, you can set each column's DataPropertyName
to the name of your property. The only trick is that you need to set the DataGridView's AutoGenerateColumns
property to false otherwise the DGV will completely ignore your manually created columns. Note that, for whatever reason, the AutoGenerateColumns
property is hidden from the Form Designer's Property Grid...no idea why. The one advantage I see to this approach is that you can pre-set the column formatting and such - you don't have to bind and then go tweak column rendering/sizing settings.
换个角度来看,我长期以来使用的方法是通过编程或通过表单设计器在 DataGridView 中预定义列。执行此操作时,您可以将每一列设置DataPropertyName
为您的属性的名称。唯一的技巧是您需要将 DataGridView 的AutoGenerateColumns
属性设置为 false,否则 DGV 将完全忽略您手动创建的列。请注意,无论出于何种原因,该AutoGenerateColumns
属性都隐藏在表单设计器的属性网格中......不知道为什么。我认为这种方法的一个优点是您可以预先设置列格式等 - 您不必绑定然后调整列渲染/大小设置。
Here's an example of what I mean:
这是我的意思的一个例子:
_DGV.AutoGenerateColumns = false;
DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn();
textColumn.DataPropertyName = "FooBar";
textColumn.HeaderText = "Foo/Bar"; // may not need to do this with your DisplayNameAttribute
_DGV.Columns.Add(textColumn);
textColumn = new DataGridViewTextBoxColumn();
textColumn.DataPropertyName = "Baz";
List<MyClass> data = GetMyData();
_DGV.DataSource = data;