Custom Sorting Not Updating Order

5 posts, 0 answers
  1. Andrew
    Andrew avatar
    104 posts
    Member since:
    May 2008

    Posted 13 Sep 2010 Link to this post

    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?
  2. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 14 Sep 2010 Link to this post

    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
  3. Andrew
    Andrew avatar
    104 posts
    Member since:
    May 2008

    Posted 17 Sep 2010 Link to this post

    Neither method works, my grid still doesn't update with the new sort order and I have no idea why not.
  4. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 20 Sep 2010 Link to this post

    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
  5. Raj
    Raj avatar
    10 posts
    Member since:
    Mar 2012

    Posted 09 Apr 2012 Link to this post

    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
Back to Top