I think there must be an attribute to hide a public property from the datagridview. But I can't find it.
Asked
Active
Viewed 3.9k times
30
-
1You can use the following link to fulfill your requirement: http://stackoverflow.com/questions/6960739/how-to-hide-column-of-datagridview-when-using-custom-datasource – user1547592 Sep 04 '12 at 04:36
3 Answers
83
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

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
-
Another option is to set DataGridAutoGeneratingColumnEventArgs.Cancel to true in AutoGeneratingColumn handler. – Aelian Dec 02 '14 at 23:06
-
3Yes, BrowsableAttribute! It's the thing I've been looking for all day. Thanks. – Szybki Mar 16 '16 at 23:29
-
1@Szybki IIRC, the only way I found out what things it looks for was by looking at reflector ... from the grid, to `PropertyDescriptor`, through to `PropertyInfo`. It isn't obvious ;p – Marc Gravell Mar 17 '16 at 08:55
-
1
-
@Daniel are you perhaps using the web control? At the time of writing, I think this question was referring to winforms, which has this property – Marc Gravell May 28 '18 at 18:28
-
Yes, I found it. The property is hidden (visible only via code, not via designer). It's called `AutoGenerateColumns`. Perhaps it would be interesting to complement the answer with this? – Daniel Möller May 28 '18 at 18:32
4
[Browsable(false)]
0
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")

Mike J
- 3,049
- 22
- 21