I made a custom DataGridView component which has a standard DataGridViewImageColumn inside. A new property changes the visibility of the column when i don\'t need in in a pa
The control is acting as expected.
Reason
You added a column in constructor, then the designer serializes the column. Then you run the application and it adds a column in constructor. So you have 2 columns. And this continues this way and is not limited to 2 columns. Even you don't need to build and run, it's enough to open the form which contains the grid and make a small change in form and save it. A new column is born!
Solutions
Based on your requirements, to solve the problem, you can consider these options:
Check if the control is in design mode, then don't add the column and add it only at run-time.
You can check if the control contains an existing such image column then don't add another one. You can make the column unique by it's type of some properties.
You can create a custom designer for the control and put the code of adding the column in initialization of control. This way the code only runs firs time you add the control to the form.
Some notes about solutions
Choosing between solutions is completely based on your requirements and all options are available. But consider these notes about solutions:
About the 1st solution, don't use DesignMode property because it doesn't work in constructor. Instead perform check this way:
if (System.ComponentModel.LicenseManager.UsageMode != LicenseUsageMode.Designtime)
{
//The control is not in design mode, add the column here.
}
About the 2nd solution, for example it's enough to add a new MyCustomImageColumn type and only check existence of a column of type MyCustomImageColumn and if it exists, don't add another one, because one of them is enough.
DataGridView has its own Designer names DataGridViewDesigner which is internal and you can not inherit from it and if you only create a designer because of this requirement you will miss some features of original designer. Probably you can workaround this or even create a ToolBoxItem instead of a designer, but you don't need it. The option is here to make the answer more complete and useful for future readers for such cases.