Reputation: 2598
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?
Upvotes: 3
Views: 2595
Reputation: 12586
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);
}
Upvotes: -1
Reputation: 6962
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
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; }
}
}
Upvotes: 2
Reputation: 2826
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.
Upvotes: 1