1

I have a windows form that contains a user control (each defined in separate assemblies). The control calls a factory method (defined in yet another assembly) to create a DataGridView CheckBox column:

datagridview.Columns.Add(DataGridViewUtilities.CreateCheckBoxColumn("Selected", false));

The code compiles and runs fine, and the control can be opened in Visual Studio 2017 Designer.

However, if I try to open the containing form in VS 2017 Designer, I get the following error:

Method not found: 'System.Windows.Forms.DataGridViewCheckBoxColumn DataGridViewUtilities.CreateCheckBoxColumn(System.String, Boolean, System.String, Boolean, System.Windows.Forms.DataGridViewAutoSizeColumnMode, System.Windows.Forms.DataGridViewColumnSortMode, Int32)'.

The callstack is:

at Control.SetupColumns(DataGridView grid) 
at Control..ctor() in ...\Control.cs:line ##

Commenting out the line that calls CreateCheckBoxColumn() allows the containing form to be opened properly in VS Designer.

CreateCheckBoxColumn() is declared as:

static public DataGridViewCheckBoxColumn CreateCheckBoxColumn(string dataPropertyName, bool readOnly = true, string columnName = null, bool threeState = false, DataGridViewAutoSizeColumnMode autosizeMode = DataGridViewAutoSizeColumnMode.None, DataGridViewColumnSortMode sortMode = DataGridViewColumnSortMode.Automatic, int width = 35)
{
...
}

A weird thing is that the control calls a similar static method defined in the same class as CreateCheckBoxColumn(), and that other method is not interfering with opening the form.

I tried rebuilding, manually deleting the contents obj & bin directories for all 3 projects, as well as the contents of AppData\Local\Microsoft\VisualStudio\15.0_6d397e1a\ProjectAssemblies, closing all open documents in VS 2017, closing the solution, and restarting Visual Studio, and it made no difference.

I tried to add a new (test) form to the same project (the project containing the form that couldn't be opened in Visual Designer), and added the offending control to it by dragging it from the toolbox. However when drop the control an error dialog comes up saying

Failed to create component 'Control'. 
The error message follows:
'System.MissingMethodException: Method not found CreateCheckBoxColumn(...)

What's wrong, and how do I fix this without having to comment out code to open the control in designer?

Jimmy
  • 5,131
  • 9
  • 55
  • 81
  • Designer of the UserControl will not show the columns in your grid. But when you open the form containing the user control, it's supposed to show columns. When you open the designer, the designer creates an instance of the base class of the class which you are designing. So the constructor of base class and child controls will run, but not the constructor of the same class which you are designing in designer. For more information about how it works, take a look at this post: [Can't view designer when coding a form in C#](https://stackoverflow.com/a/32299687/3110834) – Reza Aghaei Mar 31 '18 at 13:36
  • Thanks, question edited. – Jimmy Mar 31 '18 at 13:46
  • Did you add this control to the toolbox? Do avoid this, you get a copy of the DLL and it can very easily get out-of-sync. Letting it auto-populate is best. – Hans Passant Mar 31 '18 at 14:03
  • @HansPassant As far as I know the control got added to the toolbox automatically (is that what you mean by auto-populate?). Is there any way to verify if it was auto-populated? Or where is the copy of the dll, I suppose I could delete it to force a refresh (still better than commenting out code before opening in visual designer). – Jimmy Mar 31 '18 at 15:35
  • Try to reproduce the problem in a [MCVE]. There should not be any problem in the scenario that you described. – Reza Aghaei Mar 31 '18 at 15:57
  • @RezaAghaei Somewhat easy to reproduce for the same control. Just create a new form, try to drop the control on it, same thing happens (see question). I use this method (CreateCheckBoxColumn) in other controls in other projects and it works, but that doesn't help me solve the issue I'm facing now. – Jimmy Mar 31 '18 at 19:40
  • @JohnG Thanks for the suggestion; the code compiles & runs fine, thus DataGridViewUtilities.CreateCheckBoxColumn most certainly exists. It is not a method of System.Windows.Forms.DataGridViewCheckBoxColumn, it's a method of (my class) DataGridViewUtilities. I'm thinking it might be a dll caching issue like HansPassant suggested, though I can't find where where the cache is. – Jimmy Apr 02 '18 at 00:58

0 Answers0