Is it possible to modify the Columns property of a DataGridView (DGV) on a derived UserControl if the DGV has been defined on the 'base' UserControl?
Posted on 2011-09-23
I am trying to modify the columns of a DataGridView on a custom UserControl, call it UC_x, which derives from another custom UserControl, call it UC_base (i.e., UC_x : UC_base). The DataGridView has been placed onto UC_base in the Designer and has its 'Modifiers' property set to 'Protected Internal'.
When I open the derived UserControl, UC_x, in the Designer I see the DataGridView but I do not have access to the Columns property of the DataGridView - it is grayed-out in the Properties window - and the arrow that normally appears at the upper-right corner of a DataGridView that allows me to edit the columns (add, remove, rename, etc.) does not appear either. My goal was to create several UserControls - UC_x, UC_y, UC_z, etc. - that would all derive from UC_base and all of which would have different columns. However that does not look possible.
While investigating this issue I placed a TextBox onto UC_base and set its 'Modifiers' property to 'Protected Internal'. On UC_x I can modify the Text property of the TextBox. Other properties, e.g., the name of the TextBox, are grayed-out. This implies to me that all objects placed onto the base UserControl will have some properties that are accessible by a derived UserControl and some that are not. If this model is correct, it appears that for a DataGridView all properties, including the Columns property, fall into the group of properties that are not accessible from a derived UserControl.
Is there any way for the Columns property of a DataGridView on a derived UserControl to be modified - columns removed, columns added, columns renamed, etc.? If not, what are some suggested alternatives?
I have considered simply adding columns programmatically in the derived UserControls. Any thoughts on this?
I am writing a C# application using Microsoft Visual Studio 2010.