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

My DataGridView must support several types, and these types can have any number of public properties, not all of which I want to display.

Can anyone suggest a way to dynamically adjust the columns of a DataGridView when binding a class to a data source? Is there an attribute that will indicate the control, should the property be used as a column, for example?

Recommendations appreciated.

+4
source share
1 answer

By default (when automatic column generation is turned on), it simply receives (via ComponentModel) the [Browsable(true)] properties (or those that omit this attribute).

If this is the only use of bindings for this data, you can add [Browsable(false)] to properties that you do not want to display. Note that this will also prevent regular data binding (i.e. TextBox , PropertyGrid , etc.) to these properties.

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

+4
source

Source: https://habr.com/ru/post/1277726/


All Articles