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.”
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=284335&wa=wsignin1.0

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:

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

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

10 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
    {
    [Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]
    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:

    Genius

    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 [...]


Leave a Reply