Visual Inheritance using DataGridView

While working today I came across a perceived problem with using Visual Inheritance (Windows Forms that inherit from “base” forms).  Microsoft has admitted that it is an unsupported scenario:

“Modifying collections, such as the columns in a DataGridView, in inherited forms or controls is currently an unsupported scenario.”

Fortunately, there is a workaround that seems to fix the problems I have with it.

Create a user control class, and paste the following code:

public class ucInheritedDataGridView : DataGridView { }

Now refer to ucInheritedDataGridView every where you would have an inherited DataGridView.

22 Responses to “Visual Inheritance using DataGridView”

  1. pmz Says:

    Dear Mr. Ryan,
    First of all, THANK YOU, that’s a solution for problems – stupid VS (even 2008!) could not support the DataGridView in child form from the parent form.

    I present my C++ code of your idea:
    [Designer(“System.Windows.Forms.Design.ControlDesigner, System.Windows.Forms.Design.DLL”)]
    public ref class NLCFormDataGridView : System::Windows::Forms::DataGridView {};

    It works perfectly! Once again, thank you.

  2. Paul Says:

    Humm this doesn’t work for me… not sure why though.

    .Net 2.0,
    Visual Studio 2008,

    Attempting to do this with the DataGridView.

  3. Paul Says:

    I spoke to soon, I restarted Visual Studio and boom, it’s working now.

    Cheers for the article.

  4. Brandon Ryan Says:

    Glad to hear it worked for you Paul. Thanks for visiting and leaving a comment!

  5. Mohammad Says:

    I tried this in C# but it did not work.

    Steps I tried:
    1. added a user control.
    2. added reference to System.Design
    3. in designer source code I deleted every thing and inserted following code instead:

    using System.ComponentModel;
    using System.Windows.Forms;

    namespace Backend.DataForms
    public class ucInheritedDataGridView : DataGridView { }

    4.putted the user control on an inherited form

    it didn’t work even by restarting vs

    I’m using vs2008 with .net framework 3.5

  6. Russell Says:

    Brilliant, thanks Brandon! I was just about to give up on inherting from my base form with a DataGridView on it when I found this post. You just made my life a lot easier…

  7. Robin Says:

    Thanks, exactly what I needed!

  8. tghoang Says:

    oh, thank you very much.
    However, after this, we lose some shortcut in designer smart tag of the DataGridView.

  9. Mike Says:


    How do you guys come up with such solutions, wow.

  10. La (no) herencia visual del DataGridView - Dani PuntosNET Says:

    […] tanto no pueden modificarse en 'design time'. Hay muchas quejas al respecto aquí, aquí, aquí, aquí y aquí. Y además está reportado a Microsoft […]

  11. HaHa Says:

    Great! Thank you very much!

  12. daliborx Says:

    Mr Ryan,
    Thanks for saving my time to solve this problem…

    I attach one point for Dummies like me:

    IN VS2008/2005 go to project and Add reference to System.Designer.dll

  13. Mohammad Says:

    I added these attributes instead ones mentioned in the article:

    [Editor(“System.Windows.Forms.Design.DataGridViewComponentEditor, System.Design, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”, typeof(ComponentEditor))]
    [Designer(“System.Windows.Forms.Design.DataGridViewDesigner, System.Design, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”)]

    They worked fine in .net 4. You can find attributes appropriate for your version of .net framework using Reflector.

  14. tim Says:

    truly marvelous!

  15. Peter Says:

    I tried this and it works fine I can edit the columns with the designer. However, when I change something that updates the drawing of the grid my VS close instantly…I think it has something to do with the Datasource i am using :

  16. Peter Says:

    Finally it looks like it’s not related to my specific DataSource type. For some reason when I change the Columns collection, the designer crash and VS disappear. Anyone experienced this ?

  17. Peter Says:

    Finally my problem was some code in the UserControl Load event…I just added a if( !DesignMode) and it worked perfectly !! Thanks

  18. Says:

    A minor typo:
    1 [Designer(typeof System.Windows.Forms.Design.ControlDesigner))]
    2 public class ucInheritedDataGridView : DataGridView { }
    Should be:
    1 [Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]
    2 public class ucInheritedDataGridView : DataGridView { }

  19. Carol Maley Says:

    Awesome! Thanks a lot! It saved me from creating the sorta-same datagridview 4 times over!

  20. Bruno Logerfo Says:

    8 years later this still needs an workaround… unbelievable
    thanks a lot

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: