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

Column sort state incorrect after clearing columns

4 Answers 61 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brian Lam
Top achievements
Rank 1
Brian Lam asked on 25 Apr 2012, 03:41 PM
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.

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 26 Apr 2012, 03:52 PM
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 >>

0
Brian Lam
Top achievements
Rank 1
answered on 26 Apr 2012, 04:14 PM
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.
0
Accepted
Dimitrina
Telerik team
answered on 27 Apr 2012, 12:59 PM
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 >>

0
Brian Lam
Top achievements
Rank 1
answered on 27 Apr 2012, 03:11 PM
Thanks, that worked!
Tags
GridView
Asked by
Brian Lam
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Brian Lam
Top achievements
Rank 1
Share this question
or