1

I need a custom DataGridView class in c++/cli in visual studio 2012 that can be used in Designer View.

I created a default clr user inheriting from System::Windows::Forms::UserControl, and changed UserControl to DataGridView, but it didn't work here in C++. It works in C#. [1]

Nor codes from scratch were recognized by the Designer. [2]

It seems like I have to put the DataGridView in the class, but I will have to access its members like grid->view->GetName.. instead of grid->GetName.. now. And it wouldn't be patternized, as what CLR was intended to be after all these weird syntaxes.

[1] http://msdn.microsoft.com/en-us/library/7h62478z.aspx

[2] Adding a user control using Windows Form designer

Community
  • 1
  • 1
Haotian Yang
  • 11
  • 1
  • 2

1 Answers1

2

Follow the steps bellow for Visual Studio 2010. The steps should be valid also for Visual Studio 2012.

  1. Create a new VisualC++ -> CLR -> ClassLibrary Project (e.g. CustomDataGridView)
  2. Add the System.Windows.Forms reference into the project
  3. Change the content of the CustomDataGridView.h to:

    #pragma once
    
    using namespace System;
    using namespace System::Windows::Forms;
    
    namespace CustomDataGridView 
    {
        public ref class MyDataGridView : DataGridView
        {
            // TODO: You can include your custom behavior here.
        };
    }
    
  4. Compile your project
  5. Open/Create a project with a Form, and open that Form
  6. Right Click into the ToolBox and choose Choose Items...
  7. Browse for your CustomDataGridView.dll and load the custom control
  8. Now the MyDataGridView should be listed in the ToolBox, you can put it onto the form by Drag&Drop
Zoltan Tirinda
  • 769
  • 8
  • 25