c# Hide a property in datagridview with datasource [duplicate]

≡放荡痞女 提交于 2019-12-03 19:17:15

问题


I think there must be an attribute to hide a public property from the datagridview. But I can't find it.


回答1:


If you are adding the columns yourself... don't add the columns you don't want.

If you have AutoCreateColumns enabled, then:

  • if it is a class-based model, add [Browsable(false)] to properties you don't want
  • or set the column's .Visible to false
  • or simply remove the columns you don't want afterwards



回答2:


From 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>

[Browsable(false)]




回答3:


From your question, I would imagine you don't want to show certain "columns" within the datagridview? If so, use the Columns property to add and remove any automatically created columns that are found on the datasource which you use to attach to the grid.

The DataGridView by default will create columns for all public properties on the underlying data source object. So,

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")


来源:https://stackoverflow.com/questions/1152951/c-sharp-hide-a-property-in-datagridview-with-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!