Search Wiki:

Dynamic Data Columns Ordering


This example explains how to change the order of the table columns shown in an ASP .NET Dynamic Data site. This is a feature that is missing from the actual version of Dynamic Data, released with Visual Studio 2008 SP1. In fact, by defalult the Dynamic Data Framework shows table columns in the same order as they are defined in Data Model. For example:

Before.png

With Dynamic Data Columns Ordering, you can modify the order of the columns just by decorating them with an appropiate Custom Attribute:

After.png

The code that you can download from the Release section can be used also to hide a column in a certain page (i.e., showing some information only in the details page); this functionality is based on the example Dynamic Data - Hiding Columns in selected PageTemplates by Stephen Naughton.

The important files are the following:
  • DynamicData\CustomAttributes\ColumnMetadataAttribute.cs: it contains the Custom Attribute that must be used to specifiy the order of the column and, optionally, in which pages you want to hide it;
  • DynamicData\ExtensionMethods.cs: a class that contains an extension method that determine whether a certain column must be hidden;
  • DynamicData\ColumnsManager.cs: a class that implements the IAutoFieldGenerator and contains the logic to reorder and hide (if necessary) the columns based on the ColumnMetadata attribute.

Finally, the templates files contained in the DynamicData\PageTemplates directory must be modified in order to use the new AutoFieldGenerator. As usual, you can find the correct implementaion in the example code, but all the trick is as simple as putting two lines of code in the Page_Init event. For example, for what concerns List page:

protected void Page_Init(object sender, EventArgs e)
{
     DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
            
     // Set a custom Columns Generator for this template.
     table = GridDataSource.GetTable();
     GridView1.ColumnsGenerator = new ColumnsManager(table, PageTemplate.List);
}

And that's all. In you have any question, you can contact me at the address marco.minerva@gmail.com
Last edited Jun 7 at 12:57 PM  by marcom, version 14
Updating...
Page view tracker