Short description:
I have a UserControl with a DataGridView on it. I want to expose the DataGridView Columns collection to the designer, so I can change the columns on my User Control at design time.
Question: Which designer attributes do I need for this?
For those interested in the longer version:
I have a UserControl with the following features:
- a DataGridView that shows "pages" of items from a collection.
- a NumericUpdown control to select which page to show.
- page up / page down buttons that will disable when the first / last page is shown
- Changes to the displayed items are visually marked
- Buttons to save / discard the changes.
This user control can work autonomic. It has one function to be used by the parent control:
- Show page (collection of items to show)
The UserControl raises two events:
- Event Page changed (with a page number). Should result in loading a new page
- Event Save items (with the collection of changed items)
I have to show this user control on several forms. The only difference is that the collection of DataGridViewColumn differs per form.
I could add the columns programmatically, but it would be easier to create them using the designer.
Usually it's enough to register a suitable
UITypeEditor
usingEditor
attribute. The editor which is used by theDataGridView
isDataGridViewColumnCollectionEditor
. But in this case, if we use this editor directly, the editor expect the the property belong to aDataGridView
and tries to convert value ofITypeDescriptorContext.Instance
toDataGridVeiew
and since our editingColumns
property belongs to our user control we will receive an exception:To solve the problem, we need to create a custom
UITypeEditor
and overrideEditValue
and editColumns
property of the privateDataGridView
field of your user control.To do so, we create an instance of
ITypeDescriptorContext
containing theDataGridView
and it'sColumns
property and pass it toEditValue
method of the editor. This way the editor will edit ourColumns
property.We also decorate our property using
[DesignerSerializationVisibility]
attribute to serialize the collection contents.Here is the implementations.
MyUserControl
I suppose you add a
DataGridView
at design-time to the user control and its name would bedataGridView1
.Editor
ITypeDescriptionContext Implementation