c# - 将 DataGridView 绑定(bind)到 List<>,某些属性不应显示

标签 c# data-binding datagridview

我正在尝试将 DataGridView 绑定(bind)到一个列表,其中 MyObject 看起来像

class MyObject
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

//List<MyObject> objects;
grid.Columns[0].DataPropertyName = "Property1";
grid.DataSource = objects;

我只想显示一个属性,但我将另一列添加到我的 DataGridView 中,其中还显示了 Property2。如何防止它被添加?

最佳答案

如果您从不希望显示该属性:

class MyObject
{
    public string Property1 { get; set; }
    [Browsable(false)]
    public string Property2 { get; set; }
}

否则,如前所述 - 将 AutoGenerateColumns 设置为 false 并手动添加它们。

关于c# - 将 DataGridView 绑定(bind)到 List<>,某些属性不应显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/900126/

相关文章:

c# - 如何使用具有不同值列表的组合框单元格的文本框和组合框填充 DataGridView

c# - 将数据 GridView 导出到 csv 文件

c# - 撤销证书验证中的 X509RevocationMode.Online 问题

java - 在 GWT 中将数据绑定(bind)到 UI

winforms - 手动向 DataGridView 添加新行不会立即更新绑定(bind)的 DataTable

.net - wpf 绑定(bind)到索引器

javascript - 将对象引用传递给要在 ngModel 中使用的 Angular2 组件

c# - 接口(interface)成员是抽象的吗?

c# - 如何为列表中的每个项目添加新对象

c# - 从 C# 调试 vbscript