Windows Develop Bookmark and Share   
 index > Windows Forms Designer > DataGridView declared local, not private?
 

DataGridView declared local, not private?

Hi,

I'm making an application that uses DataGridView. While designing the layout I placed a DataGridView and named it dgrComponents.

Everything was fine, until I wanted to add a table programmically and also use it like this:

Code Snippet

if (dgrComponents.SelectedRows.Count > 0)

{

DataGridViewRow dr = dgrComponents.SelectedRows[0];

String filename = dr.Cells["colPath"].Value.ToString();

if (filename.Length > 0)

{

returnValue = new MyComponent(filename);

}

}

As you see it above, that's how I see it as well. It complains it cannot find dgrComponents.

Looking into the form.designer.cs I found this:

Code Snippet

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

System.Windows.Forms.DataGridView dgrComponents;

dgrComponents = new System.Windows.Forms.DataGridView();

((System.ComponentModel.ISupportInitialize)(dgrComponents)).BeginInit();

...

All seems well until you discover that dgrComponents is declared as a local variable to the InitializeComponent function. Which means I can't access it from my code. All other components are declared as private outside of the function.

If I move it and declare it as private, just like all the other components, it all works well......until I add/moveany component and it will remove my changes and move the dgrComponents into the function and make it local again. Which breaks my code so I have to go into the form.designer.cs and move it again.

This is extremely annoying. Anybody seen this before? Any fix?

/Xtremer

Xtremer  Wednesday, September 19, 2007 11:11 AM

Hi,

You need to changethe properties of the DataGridView as follows:

GenerateMember = true

Modifiers = private or what you need

They are all located at the Design category of the property window of the DataGridView.

The generated file of VSshould not be modified for it will be lost next time you make some changes in the designer.

Best Regards

Chunsheng Tang

Chunsheng Tang  Friday, September 21, 2007 12:49 AM

Hi,

You need to changethe properties of the DataGridView as follows:

GenerateMember = true

Modifiers = private or what you need

They are all located at the Design category of the property window of the DataGridView.

The generated file of VSshould not be modified for it will be lost next time you make some changes in the designer.

Best Regards

Chunsheng Tang

Chunsheng Tang  Friday, September 21, 2007 12:49 AM

You can use google to search for other answers

Custom Search

More Threads

• control Properties
• Group/Ungroup functionality
• cannot move control in design time
• ToolStripButtons disappear
• Serializing in RESX?
• "Could not load type from assembly" error
• Toolstrip buttons
• whats the best pattern to avoid deserialisation of usercontrol properties overwriting one another?
• MDI PARENT AND CHILD FILL PARENT AREA
• Grid dots in VS2005