ASP.NET Dynamic Data brings major usability and RAD development changes to the existing ASP.NET data controls. RAD development is significantly increased by the use of a rich scaffolding framework.
The DetailsView and GridView controls have been extended to display fields by using templates instead of by using hard-coded rules that are programmed in the controls. These templates are part of the project, and you can customize them to change their appearance or to specify which controls they use for rendering. This makes it very easy to make a change in one place in your site that specifies how to present dates for editing, as one example.
But it is not so flexible as we wanted and it does not allow default sorting on tables.
Here is a solution of how to make your ASP.NET dynamic data GridView sorted by default. This solution will require you to create an attribute - MySortByAttribute which will contain 2 proprieties: SortExpression (of type: String) and SortDirection (of type: SortDirection).
Here is the attribute code:
SortExpression - if null or empty, then the column where MySortByAttribute is put will be used to sort the GridView. Else if not empty then will contain the column or columns to sort by, it will also allow to sort by ForeignKey Columns. Example of SortExpressions: "person.surname & person.person_city.desc_"
SortDirection - will indicate how to sort - ascending or descending.
Here is the code that you need to put for a MetaForeignColumn in the DataModel:
is the code which will sort the grid which is passed as a parameter.
In this code Table is the MetaTable for which the page is displayed for.
First I loop through each column and check if it contains the attribute: MySortByAttribute. If it contains it then create the SortExpression for the GridView.
There is nothing complicated here, so if you have any questions I will be glad to help.