This is a migrated thread and some comments may be shown as answers.

Custom Sorting Not Updating Order

4 Answers 94 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 13 Sep 2010, 07:33 PM
I'm using the custom Telerik.Data.DataTable to bind my grid. I have implemented a custom DateTime sort as follows:

protected void CISGrid_Sorting(object sender, GridViewSortingEventArgs e)
{
    Telerik.Data.DataTable table;
  
    this.sortingHeaderField = CollectionUtil.GetItemFromCollectionByProperty(this.Layout.HeaderFields, "Description", e.Column.UniqueName) as HeaderField;
  
    if (this.sortingHeaderField.Control == HeaderField.ControlType.Date)
    {
        table = e.DataControl.ItemsSource as DataTable;
  
        if (e.NewSortingState == SortingState.Ascending)
        {
            table.Rows.Sort(CompareDatesAscending);
  
            e.DataControl.ItemsSource = table;
            e.Cancel = true;
        }
        else if (e.NewSortingState == SortingState.Descending)
        {
            table.Rows.Sort(CompareDatesDescending);
  
            e.DataControl.ItemsSource = table;
            e.Cancel = true;
        }
    }
}
private int CompareDatesDescending(DataRow r1, DataRow r2)
{
    return CompareDates(r1, r2, true);
}
  
private int CompareDatesAscending(DataRow r1, DataRow r2)
{
    return CompareDates(r1, r2, false);
}
  
private int CompareDates(DataRow r1, DataRow r2, bool descending)
{
    DateTime dateTime1;
    DateTime dateTime2;
  
    DateTime.TryParseExact(r1[this.sortingHeaderField.Description].ToString(), this.sortingHeaderField.InputFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dateTime1);
  
    DateTime.TryParseExact(r2[this.sortingHeaderField.Description].ToString(), this.sortingHeaderField.InputFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dateTime2);
  
    if (descending)
    {
        return DateTime.Compare(dateTime1, dateTime2);
    }
    else
    {
        return DateTime.Compare(dateTime2, dateTime1);
    }
}

Everything sorts as exepected in the DataTable but the RadGridView never updates to show the new sorting order.

Am I doing something wrong?

4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 14 Sep 2010, 01:02 PM
Hello Andrew,

I believe this is not working because the same DataTable instance is set as ItemsSource which - the ItemsSource property does not actually change. You could try setting ItemsSource to null or simply calling Rebind.

if (this.sortingHeaderField.Control == HeaderField.ControlType.Date) 
    
        table = e.DataControl.ItemsSource as DataTable; 
        // clear item source
        e.DataControl.ItemsSource = null;
    
        if (e.NewSortingState == SortingState.Ascending) 
        
            table.Rows.Sort(CompareDatesAscending); 
    
            e.DataControl.ItemsSource = table; 
            e.Cancel = true
        
        else if (e.NewSortingState == SortingState.Descending) 
        
            table.Rows.Sort(CompareDatesDescending); 
    
            e.DataControl.ItemsSource = table; 
            e.Cancel = true
        
    }
  
// or ...
  
if (this.sortingHeaderField.Control == HeaderField.ControlType.Date) 
    
        table = e.DataControl.ItemsSource as DataTable; 
    
        if (e.NewSortingState == SortingState.Ascending) 
        
            table.Rows.Sort(CompareDatesAscending); 
    
            e.Cancel = true
        
        else if (e.NewSortingState == SortingState.Descending) 
        
            table.Rows.Sort(CompareDatesDescending); 
    
            e.Cancel = true
        
  
        e.DataControl.Rebind();
    }

I hope this makes sense.


Regards,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Andrew
Top achievements
Rank 1
answered on 17 Sep 2010, 02:09 PM
Neither method works, my grid still doesn't update with the new sort order and I have no idea why not.
0
Milan
Telerik team
answered on 20 Sep 2010, 02:03 PM
Hello Andrew,

I believe that you are using our DataTable implementation. May I ask you if that is correct? I would also appreciate it if you can share the implementation of the Sort method that you are using.


Greetings,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Raj
Top achievements
Rank 1
answered on 09 Apr 2012, 06:14 PM
Hi Telerik Team,
I have one issue after Telerik Grid sort,sorting order changing in the Grid, but not updating the underlying dataset,with this i am unable to delete/update the right record from dataset.For deleting/updating iam using the e.Item.DatasetIndex.
with out using any DataKeyName for delete/update,i need the solution.

Thanks,
Raj
Tags
GridView
Asked by
Andrew
Top achievements
Rank 1
Answers by
Milan
Telerik team
Andrew
Top achievements
Rank 1
Raj
Top achievements
Rank 1
Share this question
or