Reputation: 391
On my Windows Form I have a DataGridView component, which is bound to a BindingSource. The BindingSource is an object datasource to an EntityFramework object.
Some times the columns in my DataBridView are renewed. Sometimes all properties are added as column, but now it also removed all my columns. So i've lost all my settings.
When to columns get automatically get added?
(I'm using VS.NET 2010)
Update:
//
// Summary:
// Gets or sets a value indicating whether columns are created automatically
// when the System.Windows.Forms.DataGridView.DataSource or System.Windows.Forms.DataGridView.DataMember
// properties are set.
//
// Returns:
// true if the columns should be created automatically; otherwise, false. The
// default is true.
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[DefaultValue(true)]
public bool AutoGenerateColumns { get; set; }
The property did not show up in the designer, and "hide advanced properties" is not checked.
Update 2: When I update my entity framework model, all columns are added again. I only can set the property in the constructor of the form. This is very annoying.
Upvotes: 14
Views: 30314
Reputation: 1
The little additional information provided by CoolBreeze was the item I was missing. I had created my columns in the designer so that I could do all the layout adjustments but then when I set the datasource property it would autogenerate the columns. I tried the suggestions about of setting the DataPropertyName in code but because I had the AutoGenerate set to false before doing that it was still putting the duplicate columns. Turns out I can set this property at design time as well and then set the AutoGenerate to false just prior to setting the DataSource and it works great.
Upvotes: 0
Reputation: 381
I had the same problem. I couldn't find the AutoGenerate property in my code.
For reasons I do not understand my DataGridView does not have an AutoGenerate property that I can see in my VB code.
I do not see a checkbox on the Edit Columns dialog box.
I do not see an AutoGenerate property in the grid's properties view.
I have Visual Studio Community 2017.
Here's my class properties:
Public Property BatchId As Integer
Public Property Code as String
Public Property Count As Integer
Public Property Description As String
Public Property Id As Integer
So, here's what I did:
When I ran my application the DataGridView displayed only those columns in my class.
Upvotes: 1
Reputation: 336
Set AutoGenerateColumns property to False but keep remember do it just before databinding.
eg:
DataGridView1.AutoGenerateColumns=false;
DataGridView1.DataSource=getData();
By default it is set to True.
Upvotes: 4
Reputation: 147
Try leave first of auto generated columns and set it visibility false. If it don't help try leave all them with Visible=false. Sorry for bad English.
Upvotes: 1
Reputation: 12874
Add this code or change your DataGridView Property AutoGenerateColumns
to false
DataGridView1.AutoGenerateColumns=false;
Upvotes: 7
Reputation: 56697
I actually don't know when this happens, but I tend to create all the columns manually. I create the columns in the designer and set the AutoGenerateColumns
property to false
in my code.
Upvotes: 8