I am binding a Gridview to a custom object.
But the order of fields displayed is not correct.
Is there any way to set the fields display order when binding a Gridview to a custom object?
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is not even guaranteed that the order is the same as the declaration order of the properties in your custom object as MS exposes:
“The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.”
You should order it by yourself for example with Array.Sort. Here are some further informations and also an example on hot to ensure that it’s ordered by declaration: http://www.sebastienmahe.com/v3/seb.blog/2010/03/08/c-reflection-getproperties-kept-in-declaration-order/
With Linq its even simplier:
To order your custom object it needs to implement IComparable or you have to provide an IComparer as parameter for the Sort-Function.