问题
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