Dynamic Data: Initial Sort Order for Tables and Views

Hello folks,

I’m working with Dynamic Data and wondering how to set the sort order of a Table or View list page. By default, Tables and Views have a sort of 0 (zero).

But today I was able to customize the sorting by using the DisplayColumn attribute, which provides the ability to set the column name to display and sorting for ForeignKey columns, following suggested code by Steve with a minor but essential change.

Here is the Entity definition:

using System;
using System.ComponentModel.DataAnnotations;

namespace Inorbital.ONEflyer.DataLayer
{
    [DisplayColumn("Name", "ActivityDate", true)]
    public partial class StaffActivity
    {
        public StaffActivity()
        {
            this.ActivityDate = DateTime.Now;
        }

    }

}

Now use the code below in the Page_Load of the List page (\DynamicData\PageTemplates\List.aspx.cs)

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var displayColumn = table.Attributes.OfType<DisplayColumnAttribute>().FirstOrDefault();
        if (displayColumn != null && displayColumn.SortColumn != null)
        {
            GridView1.Sort(displayColumn.SortColumn,
                displayColumn.SortDescending ? SortDirection.Descending : SortDirection.Ascending);
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s