views:

86

answers:

3

Usually when I add a control to a form using the Visual Studio GUI it writes all the code that defines the control is in a separate partial class file named ".Designer.CS". This partial class usually contains private fields for each control so the non-designer partial class can access the controls.

I've added a DataGridView in a GroupBox** to a Form. Yet a field is not being generated for the grid in the designer partial class. The control is being created correctly but no field is being defined that I can access the control from in my code.

I've tried changing the designer file so that the control is reference to a private field rather then a local variable. This works, yet I keep trying to edit the properties of the DataGridView through the Visual studio properties window, which ends up removing this field and breaking my code.

Is there a reason a private field is not setup for DataGridViews by Visual Studio in a form's designer partial class? How am I suppose to access the grid otherwise?

A: 

Did you inherit the DataGridView and make some changes to it? The form should definitely create a reference to the designer added DGV though and I'm not sure why this wouldn't be happening. In any case, you should never make any manual changes to designer generated classes as those changes will break when the designer regenerates that class.

You can always add the DataGridView manually via code to your GroupBox:

DataGridView dgv = new DataGridView(); dgv.Dock = DockStyle.Fill;

myGroupBox.Controls.Add(dgv);

Tom Frey
also, did you try creating a new UserControl add the DataGridView to it and then add that UserControl to your GroupBox.
Tom Frey
A: 

Sounds odd. How about you add your datagridview to the form again, not adjusting any of it's properties. It should be named "datagridview1" by default (assuming there is only one datagridview on the form). Now when you go back into code, does the intellisense recognize it when you enter "this."? If it does, it's probably in the Designer code somewhere, but you're just not seeing it. Just an idea.

Ken
A: 

Check that the GenerateMember property is set to true in the properties panel for the grid

Mike