views:

267

answers:

2

I've got a dynamic data app, and I want to sort based on multiple fields like so..

<DisplayColumn("FieldName", "Field1, Field2")> _

DisplayColumn doesn't seem to support more than one field?

A: 

If you want to sort gridview on multiple columns then this is how you can do -

In Page Template\List.aspx.cs & in Page_Laod event check for table name

if (!Page.IsPostBack) {
if (table.Name == "Project")
{

GridView1.Sort("ClientDetail.CompanyName asc,ProjectName asc", SortDirection.Ascending);

}

}

and you can also provide order either ascending or descending for each column.

Manoj
A: 

The DisplayColumn attribute's SortColumn param specifies the column that will be used to sort this entity when it is used as a foreign key (ex: in a DropDownList), not when it is being sorted in the GridView (or being sorted in the DB).

see SO here as well: http://stackoverflow.com/questions/731502/asp-net-dynamic-data-displaycolumn-attribute-causing-sorting-issue

msdn: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displaycolumnattribute.aspx

If this is still what you are looking for (sorting in DropDownLists only, not the DB) you can sort on multiple columns by creating a custom property in the entity that combines those two columns.

example:

[DisplayColumn("FieldName", "SortColumn")]
public partial class Employee
{
    public string SortColumn
    {
        get { return Field1 + Field2; }
    }
}
Aaron Hoffman