[英]How can i bind a generic List of custom classes to a datagridview and only show specific properties in VB.NET?
I would like a way to present custom classes in a datagridview in a custom way that I have full control of. 我想要一种以我完全控制的自定义方式在datagridview中呈现自定义类的方法。 For example if I simply bind the List to the datagridview now, a column for each public property will be created.
例如,如果我现在仅将List绑定到datagridview,将为每个公共属性创建一列。 Even if I utilize the DataPropertyName property of the DataGridViewTextBoxColumn, additional columns for each additional property of the custom class is created.
即使利用了DataGridViewTextBoxColumn的DataPropertyName属性,也会为自定义类的每个其他属性创建其他列。
I know that I can simply populate the datagridview myself, but even being a VB.NET beginner, I feel that that is not a good solution. 我知道我自己可以简单地填充datagridview,但是即使是VB.NET初学者,我也觉得这不是一个好的解决方案。 Is it?
是吗?
I think I remember that it is possible to implement some interface that helps specify exactly how a the class is represented in a datagridview control. 我想我记得,可以实现一些接口来帮助准确指定类在datagridview控件中的表示方式。
Can some one point me towards the best and most efficient solution? 有人可以将我引向最佳,最有效的解决方案吗?
DataGridView have AutoGenerateColumns
property which by default is True
. DataGridView具有
AutoGenerateColumns
属性,默认情况下为True
。 Set it to False
and specify the columns that you want under DataGridView -> Columns
tag 将其设置为
False
并在DataGridView -> Columns
标记下指定所需的列
You can make use of the Browsable
attribute . 您可以使用
Browsable
属性 。
If you bind a List of the following class to a DataGridView
如果将以下类的列表绑定到
DataGridView
Class Person
Public Property Name As String
Public Property Department As String
<System.ComponentModel.Browsable(False)>
Public Property SomethingInternal As String
End Class
only the Name
and Department
column would be created. 仅
Name
和Department
列将被创建。
Quick example: 快速示例:
Using f As New Form
Dim g = New DataGridView With {
.Dock = DockStyle.Fill,
.DataSource = { New Person With
{
.Name = "Foobar",
.Department = "BarFoo",
.SomethingInternal = "Don't show this"
}}}
f.Controls.Add(g)
f.ShowDialog()
End Using
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.