Re: DataGridView column order?



Peter,

I don't believe that this order is defined. The DataGrid view uses the
BindingManagerBase (in this case, the CurrencyManager class) to determine
what the properties on an object are. In order to do this, it calls
GetItemProperties on the BindingManagerBase to get the property information
for the grid.

Now, there are a number of options here, since you can have an
ITypedList implementation being used as well, which will have it's
GetItemProperties method called, or there can be a TypeDescriptor which
returns a custom type descriptor, and that will return the property
information.

Given all that (and there is more, I think, maybe an implementation of
ICustomTypeDescriptor as well which can return property information) it's
easy to see why there is no defined order that is given to the properties on
the grid, since there are so many different ways to obtain it.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.tveowcql8jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
I am playing around with the DataGridView control, and am curious if
anyone knows how the default order for columns is chosen when the
DataSource is a simple IList containing objects with public properties?

I assume that when you bind to an actual database data source, the order
of the columns matches the order of fields in the records in the
database. That is, the order of the columns in the table you're binding
to. But a class has no similar natural ordering per se.

I made up a test class with the public properties Name, Time, and
Northbound. They are declared in the class in that order. I would have
guessed that the DataGridView would use those properties either in
alphabetical order, or in the order that they are declared in the class
(on the assumption, possibly wrong, that the order I declare them in the
class affects how they are managed internally by the run-time). However,
the DataGridView uses neither of these. Instead, it orders them Time,
Name, and Northbound.

Using reflection, I can see that the properties are in fact at least
listed in the members of the class in the order in which they were
declared in the class. So, whatever DataGridView is doing, it's not just
pulling them out of the list in order as they appear in the collection of
members that reflection provides.

What criteria is DataGridView applying when deciding which order to
display the columns?

Pete

p.s. This is partially just an academic question, as I can of course
reorder the columns to suit my intent. I just figured if I knew how
DataGridView was deciding to order the columns, I could avoid having to
reorder them after the DataSource has been bound. And of course, I'm
curious.


.