Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView columns in designer
 

DataGridView columns in designer

I have a problem where the column order on my DataGridView doesn't stay constant. I can set the order to whatever I want in the designer and save, but when I compile, they always reorder themselves to the same thing.

The DGV is bound to a BindingSource which is bound to a class. I think I confused things by adding columns to the DGV before I bound it. Then I added a few more columns from the BindingSource via the helper.

After compiling, the columns I added via the helper (after the DGV was bound) always show up at the left, but not in the order I want them to be.

I've searched and haven't seen anything on this in here. Any help is appreciated.
Thanks
nsymms  Monday, March 27, 2006 9:28 PM

This is a known issue that sometimes occurs when databinding to business objects. The problem is that each time you compile your business object is recreated and the datasouce is recreated which causes some weird issues at design time. The workaround is to manually add the columns to the grid.

-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

Mark Rideout  Tuesday, March 28, 2006 12:30 AM
Actually, if I save and compile, the columns are reordered in the designer, but not in the code. So of course when the app runs the columns appear correctly. But the next time I edit the columns, they're in the wrong order and I have to remember re-order them before I save again or they'll be saved with the wrong order.

So it appears to be a designer issue.
nsymms  Monday, March 27, 2006 10:49 PM

This is a known issue that sometimes occurs when databinding to business objects. The problem is that each time you compile your business object is recreated and the datasouce is recreated which causes some weird issues at design time. The workaround is to manually add the columns to the grid.

-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

Mark Rideout  Tuesday, March 28, 2006 12:30 AM
Hi Mark,
what do you mean by "manually add the columns to the grid", or asked the other way, when I started building a grid with drag'n drop from the datasources, what can I do now to prevent the grid from "forgetting" the order-settings?

Thanks and regards,
Thomas

ThomaWe  Monday, April 03, 2006 3:32 PM

1) You need to goto to the DataSource property for the DataGridView and set it to the "(None)" option.
2) Next you right click on the DataGridView and select Edit Columns

3) Click the Add column button on the edit columns dialog and add as many columns as you need. Click Close when done
4) Next in the Edit Columns dialog, click each column and goto the DataPropertyName property and enter the string name of the database field/column you want the DataGridView column to be bound to.
5) Once you have specified the DataPropertyName for each column, click Ok.
6) Now Double -click your form to goto the Load event handler for the form
7) Enter code that hooks up the DataGridview's data source and sets the AutoGenerateColumsn property to false, like so:

form1_Load(...){
this.dataGridView1.AutoGeneratedColumns= false;
this.dataGridView1.DataSource = bindingSource1;
}


8) Run

-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

Mark Rideout  Tuesday, April 04, 2006 12:40 AM

Thanks much, that helped.

muga  Thursday, November 09, 2006 7:27 PM
I am encountering this problem now. I've tried different ways to solve the problem, and the only thing that works is setting the DisplayIndex at form load.

But, this is clearly a bug in VS 2005, has anything been done to correct the bug?
I have seen many threads about this subject, and no answers.
Maybe its time for some feedback...
anders_sms  Friday, October 19, 2007 12:07 PM
I agree, clearly a bug, having the same problem with manually entered column names and binding straight to a table. Problem started occurring when I started setting column header text in code. Please fix this.

rchprog  Monday, November 05, 2007 4:22 PM

This can be solved by following code:

dataGridView4.AutoGenerateColumns = false;

this.dataGridView4.Columns["Function4"].DisplayIndex = 8;

  • Proposed As Answer bySergoT Friday, February 27, 2009 5:17 PM
  •  
Gurudatt  Wednesday, March 26, 2008 8:58 AM

I have 4 tables with 30 fields in each to display on DataGridView.

It's time consuming to set DisplayIndex property for each column.

You can try this code. It worked for me.

Me.dgviewEquipment.DataSource=Nothing
Me.dgviewEquipment.Refresh()
'populatedatagridviewcontrolwithdatafromdataview
Me.dgviewEquipment.DataSource=m_dvEquipmentInventory
Me.dgviewEquipment.Refresh()

If you want to hide any columns

Fori=27TodgviewEquipment.ColumnCount-1
Me.dgviewEquipment.Columns(i).Visible=False
Next

Hope this will help someone and save your time.

  • Proposed As Answer bypaul2411 Wednesday, September 09, 2009 5:19 PM
  •  
SergoT  Friday, February 27, 2009 5:26 PM
Many thanks.
Setting the DataSource to null between bindings fixed it for me. Completely impractical to add columns manually for my app - the same grid displays nany hundreds of different run-time generated datasets.
It seems what is happening is that when you bind 'on top of' another datasource, any common columns are reused, and therefore shuffled along to the left as new columns are added on the right.

paul
paul2411  Wednesday, September 09, 2009 5:18 PM

You can use google to search for other answers

Custom Search

More Threads

• more events
• Datagrid event for cell content change?
• Why do I have to detach the database each time I run a project?
• In DGV, best place to put EndEdit & TableAdapter Update
• ;how to update all at a time
• How to Rolls back changes after a update method failed within a transaction with multiple tables updates?
• how to do restrict char in datagridviewcell?
• load combo from stored procedure
• Select rows using keyboard in DataGridView
• How to edit a date to null from a textbox