I was able to achive what I needed by doing the following:
I create the Custom Attribute SortManyColumnsAttribute
[AttributeUsage(AttributeTargets.Class)]
public class SortManyColumnsAttribute : Attribute
{
public String sortColumn { get; private set; }
public SortManyColumnsAttribute(String sortColumns)
{
sortColumn = sortColumns;
}
}
I added the Attribute to my table in question with the string comma separated value representing the [entity].[columnname] to sort:
[MetadataType(typeof(ScopeOfWork2WorkItemMetadata))] [SortManyColumns("ScopeOfWork.Description, WorkItem.Description")] public partial class ScopeOfWork2WorkItem
3. Then modified the extender class SetInitialSortOrder as follows (forgive the programming is raw and needs to be cleaned up):
string attrs = table.Attributes.OfType<SortManyColumnsAttribute>().Select(s => s.sortColumn).FirstOrDefault();
if(!String.IsNullOrEmpty(attrs))
{
String[] sort = attrs.Split(',').ToArray();
OrderByExpression order = new OrderByExpression()
{
DataField = sort[0].Trim(),
Direction = SortDirection.Ascending,
};
for(int i = 1; i < sort.Count(); i++)
{
ThenBy then = new ThenBy
{
DataField = sort[i].Trim(),
Direction = SortDirection.Ascending
};
order.ThenByExpressions.Add(then);
}
queryExtender.Expressions.Add(order);
}
bjrivera
Member
14 Points
17 Posts
Re: Sorting 2 columns in List.aspx
Apr 25, 2012 08:32 PM|LINK
I was able to achive what I needed by doing the following:
[AttributeUsage(AttributeTargets.Class)] public class SortManyColumnsAttribute : Attribute { public String sortColumn { get; private set; } public SortManyColumnsAttribute(String sortColumns) { sortColumn = sortColumns; } }[MetadataType(typeof(ScopeOfWork2WorkItemMetadata))]
[SortManyColumns("ScopeOfWork.Description, WorkItem.Description")]
public partial class ScopeOfWork2WorkItem
3. Then modified the extender class SetInitialSortOrder as follows (forgive the programming is raw and needs to be cleaned up):
string attrs = table.Attributes.OfType<SortManyColumnsAttribute>().Select(s => s.sortColumn).FirstOrDefault(); if(!String.IsNullOrEmpty(attrs)) { String[] sort = attrs.Split(',').ToArray(); OrderByExpression order = new OrderByExpression() { DataField = sort[0].Trim(), Direction = SortDirection.Ascending, }; for(int i = 1; i < sort.Count(); i++) { ThenBy then = new ThenBy { DataField = sort[i].Trim(), Direction = SortDirection.Ascending }; order.ThenByExpressions.Add(then); } queryExtender.Expressions.Add(order); }