Column sort state incorrect after clearing columns

5 posts, 1 answers
  1. Brian Lam
    Brian Lam avatar
    28 posts
    Member since:
    Apr 2010

    Posted 25 Apr 2012 Link to this post

    Hi,

    In our application, we sometimes have to clear out the Columns collection and then repopulate it from scratch based on some user settings. However, after calling Columns.Clear() and then repopulating the columns, the column headers don't reflect the correct sorting state, even though the data is still correctly sorted and the SortDescriptors collection has a sorting on the column.

    For example, let's say in our MainPage's constructor I add a column bound to the ID property:

    public MainPage()
    {
        InitializeComponent();
     
        GridView.AutoGenerateColumns = false;
     
        var column = new GridViewDataColumn()
        {
            DataMemberBinding = new Binding("ID"),
        };
        GridView.Columns.Add(column);
     
        Button.Click += Button_Click;
    }

    While the application is running, I click the column header to sort the data. (see columnsorting1.png)

    In Button's click handler, I clear the Columns collection and then re-add a new column bound to ID:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        GridView.Columns.Clear();
     
        var column = new GridViewDataColumn()
        {
            DataMemberBinding = new Binding("ID"),
        };
     
        GridView.Columns.Add(column);
    }

    When I click the button, the new ID column header shows no sorting state, even though there's a SortDescriptor that sorts on ID. (see columnsorting2.png)

    I was able to reproduce the issue in the 2012.1 326 release.
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 26 Apr 2012 Link to this post

    Hello,

     The sort state of the column is incorrect because the ColumnSortDescriptor cannot associate with the newly created column. You can change your code like so:

    playersGrid.Columns.Clear();
     
    var column = new GridViewDataColumn()
    {
         DataMemberBinding = new Binding("Name"),
    };
     
    playersGrid.Columns.Add(column);
    (playersGrid.SortDescriptors[0] as ColumnSortDescriptor).Column = column;

    I hope that helps.

    All the best,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Brian Lam
    Brian Lam avatar
    28 posts
    Member since:
    Apr 2010

    Posted 26 Apr 2012 Link to this post

    Hi Didie,

    Is there a similar solution for programmatically added sort descriptors (i.e. SortDescriptors, not ColumnSortDescriptors)? Programmatically added SortDescriptors don't have a Column property, but RadGridView columns that are bound to the same property as the SortDescriptor update their visual state to match the SortDescriptor. However, if I call Columns.Clear() and re-add the columns like in my original post, I have the same issue, even though the SortDescriptors are still intact.
  5. Answer
    Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 27 Apr 2012 Link to this post

    Hi,

     Indeed, you are right. The SortDescriptors have to be re-applied. You can call the Reset() method of the RadGridView.SortDescriptors collection.

    Regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. Brian Lam
    Brian Lam avatar
    28 posts
    Member since:
    Apr 2010

    Posted 27 Apr 2012 Link to this post

    Thanks, that worked!
Back to Top
DevCraft banner