3

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?

NoCarrier
  • 2,558
  • 4
  • 33
  • 44

3 Answers3

2

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: 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; }
    }
}
Community
  • 1
  • 1
Aaron Hoffman
  • 6,604
  • 8
  • 56
  • 61
1

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 Attal
  • 2,806
  • 3
  • 28
  • 31
-1

Yes it does support multiple field. You can try this:

[DisplayColumn("FieldName", "Field1 ASC, Field2 DESC, Field3")]

Then in Page_Load of List.aspx:

var displayColumn = table.GetAttribute<DisplayColumnAttribute>();
if (displayColumn != null && displayColumn.SortColumn != null)
{
    GridView1.Sort(displayColumn.SortColumn,
    displayColumn.SortDescending ? SortDirection.Descending : SortDirection.Ascending);
}
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137
  • Sure, this will work if you manually sort, but it pretty much messes up Dynamic Data in general and causes yellow screens for any attempted automatic sorts, so it's a bad idea. – Aaronaught Feb 09 '13 at 20:58
  • @Aaronaught, what will be a better way? – Ray Cheng Feb 09 '13 at 21:14
  • 1
    Aaron's answer works if performance isn't a concern. Otherwise I'd use a computed column which can be indexed on the server. It would be nice if DD just plonked whatever string you choose into an `ORDER BY` but unfortunately that's not what it does, and making this work ultimately ends up being more hassle than just working within the framework by synthesizing a single column. – Aaronaught Feb 10 '13 at 02:17