简体   繁体   中英

Strange VS2010 behavior with datagrid and designer

I am working on a winforms app with a DataGridView control on it, and I am experiencing some frustrating things.

First off, I want to turn off AutoColumnGeneration, but it's not listed in the properties. No problem, I can do that with a line of code...and this is where it gets weird:

  1. In my code, the DataGridView is inaccessible. Its like it doesnt exist on the form. Looking into this, its because the designer is declaring it as part of the InitializeComponent() method instead of where it initializes all the other controls.

  2. Because its in the designer, any change I make there gets reversed so I can't fix this.

Is there any way to stop visual studio from doing this? I found a hack around it by using one of the datagrid columns (which ARE accessible) to create a reference to the datagridview its associated with and access it that way. It works, but its ugly and not intuitive at all.

I think I found it:

In the designer, click on the DataGridView control, and change the property of GenerateMember to true. I'm guessing it is set to false.

That property is used to do just that: hide the control from the code windows. It's useful for Labels or ToolStripSeparators that you don't need to deal with in code.

I personally use the binding source as the datasource which can even be an object and then under columns it will list all of the available columns and you can pick and choose which ones are visible as well as a slew of other options including formatting.

Click the drop down on the datasource and Add a new data source and select the necessary object, in my case an order detail object. Here is some of my designer code which is created by VS2010

this.dgvOrderDetails.DataSource = this.orderDetailBindingSource;
this.orderDetailBindingSource.DataSource = typeof(OrderDetail);

And the binding source code that sets up the data to fill the datagridview (I coded this part)

orderDetailBindingSource.DataSource = orderDetList;

Then just click the ellipses on the Columns property of the datagridview and it will have all the columns listed that are available from the object and I set the ones I want visible, the order, format etc.

As for the 2nd issue I don't think you'll have that problem once you use the designer to make the changes I listed above.

In my case, I declared a private property in the Form's partial class (the file for my code, not the Designer's file) to return the control by navigating through the Controls hierarchy.

private DataGridView MyGrid
{
    get { return (DataGridView)this.Controls[0].Controls[1].Controls[0].Controls[1].Controls[0]; }
}

I agree, there ought to be a better way, such as Visual Studio Designer declaring the control like it does most other controls on the form. In the meantime, this works.

Warning!

If the form's control hierarchy is ever changed, the property's definition will have to be manually updated.

Edit

I have a better solution - at least in Visual Studio 2012.

While in the form Designer, with the DataGridView selected, open its properties and look for the GenerateMember property (under the Design node) and ensure it is set to True . Once set to True , the Designer will declare a member variable for the DataGridView control.

The strange thing is that the default value appears to be True , so I'm curious how it was changed to False ? Perhaps I mis-clicked when setting the name?

By the way @LarsTech's answer is the same as this update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM