c#使用datasource隐藏datagridview中的属性

时间:2009-07-20 11:12:49

标签: c# attributes datasource

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

3 个答案:

答案 0 :(得分:63)

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

如果您启用了AutoCreateColumns,那么:

  • 如果是基于类的模型,请将[Browsable(false)]添加到您不想要的属性
  • 或将列的.Visible设置为false
  • 或者只是删除之后不想要的列

答案 1 :(得分:3)

答案 2 :(得分: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")
相关问题