/// <summary>
/// Sets the initial sort order.
/// </summary>
/// <param name="queryExtender">The query extender.</param>
/// <param name="table">The table.</param>
public static void SetInitialSortOrder(this QueryExtender queryExtender, MetaTable table)
{
// set default sort
if (table.SortColumn != null)
{
var order = new OrderByExpression()
{
DataField = table.SortColumn.Name,
Direction = table.SortDescending ? SortDirection.Descending : SortDirection.Ascending,
};
queryExtender.Expressions.Add(order);
}
}
use it like this in the List page
// set initial sort order
GridQueryExtender.SetInitialSortOrder(table);
Obviousely I have not update to handle multiple columns but I don't think that will be a big issue all you will need to do for columns after the first one is to add ThenByExpressions to the OrderByExpression.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
I implemented the extended class SetInitialSortOrder for sorting a single column and it works will when the column that needs to be sorted is not a foreign key, otherwise it throws an exception.
Also, when modifing this implementaion to sort multiple columns, I ran into an issue with defining more than one column in the DisplayColumn annotation. It throws and exception and does not like this.
if it is a foreign Key column you will need to use the actual column in the DB not the navigation property
i.e. if you have a relationship between Products and Categories in the DB based on the Categories PK and the FK column in Products of CategoriesId the in DD you would see the Categories colum this is the navigation property you will need to filter on the
CategoriesId column usign the PK value from the Categories table.
Hope that made sense :)
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
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);
}
Hi ValZ, not sure what you mean, do you mean after the page loads and you click a column to change the sort? if yes then that overieds the default sort and since out of the box the GridView does not support multi column search you will need to extend it
a little see
http://www.codeproject.com/Articles/14437/Extending-GridView-by-allowing-Multiple-Selections that may help.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
bjrivera
Member
14 Points
17 Posts
Sorting 2 columns in List.aspx
Apr 25, 2012 04:29 AM|LINK
I need to be able to sort more than one column within the List.aspx page.
I am currently able to sort a column defined in the 2nd parameter in DisplayColumn as shown below from the List.aspx.cs (from http://csharpbits.notaclue.net/2011/01/setting-initial-sort-order-dynamic-data.html):
tusharrs
Contributor
3230 Points
668 Posts
Re: Sorting 2 columns in List.aspx
Apr 25, 2012 04:34 AM|LINK
hi,
bind the grid with dataview before binding sort dataview with multiple columns like this
Here dt is your datatable which contains the data
DataView view = dt.DefaultView;
view.Sort = "Column1 asc, Column2 desc";
GridView1.DataSource = view.ToTable();
GridView1.DataBind();
( Mark as Answer if it helps you out )
View my Blog
valZ
Member
130 Points
41 Posts
Re: Sorting 2 columns in List.aspx
Apr 25, 2012 07:51 AM|LINK
I am doing like that
protected void GridDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e) { LinqDataSource GridDataSource = (LinqDataSource)GV.FindDataSourceControl(); GridDataSource.OrderBy = "Column1 asc, Column2 desc"; }sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Sorting 2 columns in List.aspx
Apr 25, 2012 09:05 AM|LINK
Hi bjrivera, try this instead;
/// <summary> /// Sets the initial sort order. /// </summary> /// <param name="queryExtender">The query extender.</param> /// <param name="table">The table.</param> public static void SetInitialSortOrder(this QueryExtender queryExtender, MetaTable table) { // set default sort if (table.SortColumn != null) { var order = new OrderByExpression() { DataField = table.SortColumn.Name, Direction = table.SortDescending ? SortDirection.Descending : SortDirection.Ascending, }; queryExtender.Expressions.Add(order); } }use it like this in the List page
// set initial sort order GridQueryExtender.SetInitialSortOrder(table);Obviousely I have not update to handle multiple columns but I don't think that will be a big issue all you will need to do for columns after the first one is to add ThenByExpressions to the OrderByExpression.
Always seeking an elegant solution.
bjrivera
Member
14 Points
17 Posts
Re: Sorting 2 columns in List.aspx
Apr 25, 2012 11:48 AM|LINK
Thank you sjnaughton. I will let you know how it works out.
bjrivera
Member
14 Points
17 Posts
Re: Sorting 2 columns in List.aspx
Apr 25, 2012 02:03 PM|LINK
Hi sjnaughten:
I implemented the extended class SetInitialSortOrder for sorting a single column and it works will when the column that needs to be sorted is not a foreign key, otherwise it throws an exception.
Also, when modifing this implementaion to sort multiple columns, I ran into an issue with defining more than one column in the DisplayColumn annotation. It throws and exception and does not like this.
Is there a known solution for this?
Thank you for your assistance.
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Sorting 2 columns in List.aspx
Apr 25, 2012 04:24 PM|LINK
if it is a foreign Key column you will need to use the actual column in the DB not the navigation property
i.e. if you have a relationship between Products and Categories in the DB based on the Categories PK and the FK column in Products of CategoriesId the in DD you would see the Categories colum this is the navigation property you will need to filter on the CategoriesId column usign the PK value from the Categories table.
Hope that made sense :)
Always seeking an elegant solution.
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); }valZ
Member
130 Points
41 Posts
Re: Sorting 2 columns in List.aspx
Apr 26, 2012 07:10 AM|LINK
But this works only when initializing the Grid, and what about the Grid Sorting by ScopeOfWork.Description column?
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Sorting 2 columns in List.aspx
Apr 26, 2012 11:22 AM|LINK
Hi ValZ, not sure what you mean, do you mean after the page loads and you click a column to change the sort? if yes then that overieds the default sort and since out of the box the GridView does not support multi column search you will need to extend it a little see http://www.codeproject.com/Articles/14437/Extending-GridView-by-allowing-Multiple-Selections that may help.
Always seeking an elegant solution.