How to dynamically set which properties get bound to a DataGridView?

.net, c#, data-binding, datagridview, winforms

Solution

By default (with auto column generation enabled), it will simply obtain (via ComponentModel) the `[Browsable(true)]` properties, (or those that omit this attribute).

If this is the only use of binding for this data, you could add `[Browsable(false)]` to the properties you don't want to show. Note that this will also prevent regular data-binding (i.e. `TextBox`, `PropertyGrid`, etc) to those properties.

In reality, I expect it would be better to create your own attribute, and use that to find the properties you want to display via reflection (and build the columns yourself).

Problem

My DataGridView needs to support a number of types and these types may have any number of public properties, not all of which I want to display. Can anyone suggest a way to dynamically customise a DataGridView's columns when binding a class a datasource? Is there an attribute that will tell a control whether to use a property as a column for example? Advice appreciated.

Original source