Sorting by multiple fields using DisplayColumnAttribute in Asp.Net Dynamic Data App?
I've got a dynamic data app, and I want to sort based on multiple fields like so..
开发者_JS百科<DisplayColumn("FieldName", "Field1, Field2")> _
DisplayColumn doesn't seem to support more than one field?
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; }
}
}
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.
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);
}
精彩评论