28

我认为必须有一个属性来隐藏datagridview的公共属性。但我找不到它。

4

3 回答 3

77

如果您自己添加列...不要添加您不想要的列。

如果您已AutoCreateColumns启用,则:

  • 如果它是基于类的模型,请添加[Browsable(false)]到您不想要的属性
  • 或将列设置.Visible为 false
  • 或者干脆删除你不想要的列之后
于 2009-07-20T11:19:30.780 回答
4

Is there an Attribute I can use in my class to tell DataGridView not to create a column when bound to a List<MyClass>

[可浏览(假)]

于 2009-07-20T11:15:08.187 回答
0

根据您的问题,我想您不想在 datagridview 中显示某些“列”?如果是这样,请使用 Columns 属性添加和删除在用于附加到网格的数据源上找到的任何自动创建的列。

默认情况下,DataGridView 将为基础数据源对象上的所有公共属性创建列。所以,

public class MyClass
{
   private string _name;

   public string Name
   {
      get{ return _name; }
      set { _name = value; }
   }

   public string TestProperty
   {
      { get { return "Sample"; }
   }
}

...
[inside some form that contains your DataGridView class]

MyClass c = new MyClass();

// setting the data source will generate a column for "Name" and "TestProperty"
dataGridView1.DataSource = c;

// to remove specific columns from the DataGridView
// dataGridView1.Columns.Remove("TestProperty")
于 2009-07-20T11:32:50.443 回答