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.

5 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. 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…


Leave a Reply