P
Peter Duniho
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.
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.