Hello there
I am trying to override the WebGrid Sorting. Is this possible? I am using the method SortColumn and my code is working, but it seems that the default sorting of the webgrid is also running, as it re-sorts my results.
I am passing the SortOrder as a variable through the session to my stored procedure. This has to be so in my case, as my Stored Procedure uses "Row_Number Over" function in SQL Server 2005 and hence I need to pass the sort column seperately.
Can I turn off the WebGrid sorting without disabling sorting altogether? What is the best solution for writing my own sorting code?
Your implementation works great for a single band. However, once I tried to run the code and sort a second band that is a child of the first, the row disappears when I click on a header.
base.OnInit(e);
uwTimeSheet.DisplayLayout.SortingAlgorithmDefault = SortingAlgorithm.Custom;
uwTimeSheet.DisplayLayout.SortImplementation = new SortRowsCollection(SortRows);
}
{
e.Layout.HeaderClickActionDefault = HeaderClickAction.SortSingle;
etc...
I also have a function in my code similar to SortRows, and a class similar to SortedItem, above, but it is not called on second band header clicks. How come? Please help!
Hi,
Your solution works for me also, but I am not able to perform sort for the second time on the same column. Can you please let me know if I am missing anything.
Thanks in Advance.
Do you mean change the icons from ascending to descending or do you mean that you want to change the actual images used?
The column objects have a property called SortIndicator that you can set to display either ascending, descending or none.
The actual images used can be changed by setting the path to the images you want to use to the grid's DisplayLayout.Images property.
Thanks a lot for your reply. This has helped me a lot.
My last trouble with this issue is the icon. Is it possible to change the sorting icon at the top at runtime? If at all possible please provide syntax in VB.NET. Thank you
To disable the grid's sort you need to set the Cancel property of the event argument to true and then clear the sorted columns collection. You then will need to set up the sort indicators again in the page's prerender event. There's a knowledge base article that shows what you need to do.
If you're using 7.1 or above, you might want to look at using the grid's DisplayLayout.SortingAlgorithmDefault and DisplayLayout.SortImplementation properties to provide a delegate method to handle the sorting whenever the grid needs to be sorted.
To make it work you need to set the SortingAlgorithmDefault to Custom and supply a sort implementation method delegate in the page's Init stage before the grid starts to do any data binding:
protected override void OnInit(EventArgs e){ base.OnInit(e); UltraWebGrid1.DisplayLayout.SortingAlgorithmDefault = SortingAlgorithm.Custom; UltraWebGrid1.DisplayLayout.SortImplementation = new SortRowsCollection(SortRows);}
Then you can put whatever logic you want into the method. Here's a sample one that sorts based on the alphabetical sort order of the words once they've been reversed:
public void SortRows(RowsCollection rows, int[ indices){ List<SortedItem> list = new List<SortedItem>(); foreach (UltraGridRow row in rows) { list.Add(new SortedItem(row.Index, ReverseWords(row.Cells[UltraWebGrid1.Bands[0].SortedColumns[0].Index].Value.ToString()))); } list.Sort(); for (int x = 0; x < list.Count; x++) { if (UltraWebGrid1.Bands[0].SortedColumns[0].SortIndicator == SortIndicator.Descending) indices[x] = list[(list.Count - 1) - x].OriginalIndex; else indices[x] = list[x].OriginalIndex; }}
private class SortedItem : IComparable{ int _originalIndex; string _value; public SortedItem(int index, string value) { this.OriginalIndex = index; this.Value = value; } public int OriginalIndex { get { return _originalIndex; } set { _originalIndex = value; } } public string Value { get { return _value; } set { _value = value; } } public int CompareTo(object obj) { if (obj is string) return Value.CompareTo((string)obj); if (obj is SortedItem) return Value.CompareTo(((SortedItem)obj).Value); throw new ArgumentException("Can only compare SortedItem objects to other SortedItem objects or to strings"); }}