DataGridView columns being reordered

G

Guest

I have a DataGridView which I have configured a set of columns and their fill
weights in the designer. Also, I have specified a DataPropertyName for each
column. I programmatically create a BindingSource to hold a set of objects
that I want to display in the DataGridView. I then assign the BindingSource
as the DataSource of the grid view.

The binding works ok and the data displays in the grid view. However, it
will randomly (?) rearrange a column in the grid view. So if my columns were
1, 2, 3, 4, it would end up as 1, 2, 4, 3. But all the data is in the right
place. Does anyone know what would cause this and how it can be prevented?
 
B

Bob Powell [MVP]

Recent changes to the TypeDescriptor mean that properties are not
necessarily ordered in the same way every time. Automatic generation of
columns depends on property order in the list of PropertyDescriptor objects
but I'm not sure as to the situation with columns created in the designer.
Maybe they follow property order to some extent too.

You can specify that the grid should not auto-generate columns and
explicitly create them yourself or you can create a custom property order
using the new TypeDescriptionProvider system and order your properties in
any way you desire.

In an implementation that I designed I defined an Index attribute that
enables me to sort properties that come from the TypeDescriptor into a
specific numerical order. I'd give you code but it's for a client but
hopefully you get the picture... These solutions are heavily based on a good
knowledge of reflection.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top