Business object databinding

H

hharry

Hello All,

I am binding a custom business object the gridview control.

When I bind a datatable to the gridview control, the columns are
displayed in the same order as the select statement used to populate
the datatable.

When I bind a custom business object to the gridview control, the
column order is not the same as the order in which the business object
properties are declared and I need to modify the columns collection.

Could someone briefly explain why this occurs ??


Thanks in advance!
 
T

Tim Van Wassenhove

hharry schreef:
When I bind a datatable to the gridview control, the columns are
displayed in the same order as the select statement used to populate
the datatable.

When I bind a custom business object to the gridview control, the
column order is not the same as the order in which the business object
properties are declared and I need to modify the columns collection.

(Imho) the easiest way to work around this is to implement ITypedList on
the collection of business objects that you bind too...

(A couple of demonstrations of that technique can be found at
http://www.timvw.be/control-the-order-of-properties-in-your-class/)
 
M

Marc Gravell

For reference - ITypedList isn't the only way... I'm not very
conversant in GridView (using DataGridView instead), but can you not
tweak the columns manually?

But rather than ITypedList, you could perhaps use a
TypeDescriptionProvider; all that is then needed is for the data-
source to have a typed indexer SomeClass this[int] (often T this[int]
for a generic), which means it will work out-of-the-box for List<T>,
BindingList<T>, etc. You can use this approach even on data-entity
classes to which you don't have source code access. The implementation
approach (changing the PropertyDescriptorCollection order) is the
same. Let me know if you want any more details (or google for
"HyperDescriptor" for an unrelated example).

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

hharry said:
Hello All,

I am binding a custom business object the gridview control.

When I bind a datatable to the gridview control, the columns are
displayed in the same order as the select statement used to populate
the datatable.

When I bind a custom business object to the gridview control, the
column order is not the same as the order in which the business object
properties are declared and I need to modify the columns collection.

Could someone briefly explain why this occurs ??

The compiler does not enforce the order in wich the properties are declared
in a class. When you use binding the properties are discovered using
Reflection. You cannot relay in a particular order.

What if you use BindingSource instead of directly binding it to the
collection?
 
T

Tim Van Wassenhove

Marc Gravell schreef:
But rather than ITypedList, you could perhaps use a
TypeDescriptionProvider;

Thanks, i didn't know about this class yet.. Time to explore ;)
 

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