7

I have a WinForms form with a DataGridView on it. The DataGridView is set to protected.

When I inherit that form, it is not possible to change the DataGridView in the Visual Studio designer. If I do the whole thing with a Button, it works as expected.

Is there a way to fix this?

Some (clipped) code (from DatagridForm.Designer.cs):

partial class DatagridForm {
    protected DataGridView dgData;
    protected Button button;
}

and from Inherited.cs:

partial class Inherited : DatagridForm {

}
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

1 Answers1

9

This blog has the solution: just create an inherited user control that points to the right designer stuff:

[Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]
public class ucInheritedDataGridView : DataGridView { }

Works like a charm. Only downside is that you cannot use the .NET Client Profile, because it has no support for System.Forms.Design (you have to add System.Design as a reference). This shouldn't be too much of a problem, because the client profile is deprecated anyway per 4.5.

If you really need the client profile, another work around is to wrap the DataGridView in a Panel, which you can move and resize.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195