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>

Blorgbeard is out picture Blorgbeard is out · Jul 1, 2009 · Viewed 20.9k times · Source

I have a class like this:

private class MyClass {
  [DisplayName("Foo/Bar")]
  public string FooBar { get; private set; }
  public string Baz { get; private set; }      
  public bool Enabled;
}

When I create a List<MyClass> and assign it to the DataSource of a DataGridView, the grid shows me two columns, "Foo/Bar" and "Baz". This is what I want to happen.

It currently works because Enabled is a field, not a property - DataGridView will only pick up properties. However, this is a dirty hack.

I would like to make Enabled a property too, but still hide it on the DataGridView.

I know I can manually delete the column after binding.. but that's not ideal.

Is there an attribute similar to DisplayName, that I can tag a property with? Something like [Visible(false)] ?

Answer

C-Pound Guru picture C-Pound Guru · Jul 1, 2009

[Browsable(false)] will hide a property from a DataGridView.

A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true. These members can be modified at design time. Members marked with the BrowsableAttribute constructor's browsable parameter set to false are not appropriate for design-time editing and therefore are not displayed in a visual designer. The default is true.