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

[Solved] Sort being cleared when ItemsSource is updated.

4 Answers 118 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris
Top achievements
Rank 1
Chris asked on 20 Jul 2010, 11:36 PM
I'm having a problem with the sort descriptors being cleared from the grid when my data source is updated. Here is a sample I created to recreate the problem.

<UserControl x:Class="RedHeaderTest.MainPage"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Button Content="Hookup Data" Width="Auto" Margin="4,2,4,2" Click="Button_Click"/>
        </StackPanel>
        <telerik:RadGridView Margin="5" Grid.Row="1" AutoGenerateColumns="False" x:Name="ParentGrid" ShowGroupPanel="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Width="100" UniqueName="Col1" DataMemberBinding="{Binding Path=Col1}">
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="Col1" TextAlignment="Left" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Width="100" UniqueName="Col2" DataMemberBinding="{Binding Path=Col2}">
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="Col2" TextAlignment="Left" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Width="100" UniqueName="Col3" DataMemberBinding="{Binding Path=Col3}">
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="Col3" TextAlignment="Left" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Width="100" UniqueName="Col4" DataMemberBinding="{Binding Path=Col4}">
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="Col4" TextAlignment="Left" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace RedHeaderTest
{
    public partial class MainPage : UserControl
    {
        private List<ParentData> _data;
 
        public MainPage()
        {
            this._data = new List<ParentData>();
            this._data.Add(new ParentData()
            {
                Col1 = "Row1 Col1",
                Col2 = "Row1 Col2",
                Col3 = "Row1 Col3",
                Col4 = "Row1 Col4",
                ChildData = new List<ChildData>(
                    new ChildData[] {
                        new ChildData()
                        {
                            Col1 = "ParentRow1 Row1 Col1",
                            Col2 = "ParentRow1 Row1 Col2",
                            Col3 = "ParentRow1 Row1 Col3",
                            Col4 = "ParentRow1 Row1 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow1 Row2 Col1",
                            Col2 = "ParentRow1 Row2 Col2",
                            Col3 = "ParentRow1 Row2 Col3",
                            Col4 = "ParentRow1 Row2 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow1 Row3 Col1",
                            Col2 = "ParentRow1 Row3 Col2",
                            Col3 = "ParentRow1 Row3 Col3",
                            Col4 = "ParentRow1 Row3 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow1 Row4 Col1",
                            Col2 = "ParentRow1 Row4 Col2",
                            Col3 = "ParentRow1 Row4 Col3",
                            Col4 = "ParentRow1 Row4 Col4",
                        }
                    })
            });
            this._data.Add(new ParentData()
            {
                Col1 = "Row2 Col1",
                Col2 = "Row2 Col2",
                Col3 = "Row2 Col3",
                Col4 = "Row2 Col4",
                ChildData = new List<ChildData>(
                    new ChildData[] {
                        new ChildData()
                        {
                            Col1 = "ParentRow2 Row1 Col1",
                            Col2 = "ParentRow2 Row1 Col2",
                            Col3 = "ParentRow2 Row1 Col3",
                            Col4 = "ParentRow2 Row1 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow2 Row2 Col1",
                            Col2 = "ParentRow2 Row2 Col2",
                            Col3 = "ParentRow2 Row2 Col3",
                            Col4 = "ParentRow2 Row2 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow2 Row3 Col1",
                            Col2 = "ParentRow2 Row3 Col2",
                            Col3 = "ParentRow2 Row3 Col3",
                            Col4 = "ParentRow2 Row3 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow2 Row4 Col1",
                            Col2 = "ParentRow2 Row4 Col2",
                            Col3 = "ParentRow2 Row4 Col3",
                            Col4 = "ParentRow2 Row4 Col4",
                        }
                    })
            });
            this._data.Add(new ParentData()
            {
                Col1 = "Row3 Col1",
                Col2 = "Row3 Col2",
                Col3 = "Row3 Col3",
                Col4 = "Row3 Col4",
                ChildData = new List<ChildData>(
                    new ChildData[] {
                        new ChildData()
                        {
                            Col1 = "ParentRow3 Row1 Col1",
                            Col2 = "ParentRow3 Row1 Col2",
                            Col3 = "ParentRow3 Row1 Col3",
                            Col4 = "ParentRow3 Row1 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow3 Row2 Col1",
                            Col2 = "ParentRow3 Row2 Col2",
                            Col3 = "ParentRow3 Row2 Col3",
                            Col4 = "ParentRow3 Row2 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow3 Row3 Col1",
                            Col2 = "ParentRow3 Row3 Col2",
                            Col3 = "ParentRow3 Row3 Col3",
                            Col4 = "ParentRow3 Row3 Col4",
                        },
                        new ChildData()
                        {
                            Col1 = "ParentRow3 Row4 Col1",
                            Col2 = "ParentRow3 Row4 Col2",
                            Col3 = "ParentRow3 Row4 Col3",
                            Col4 = "ParentRow3 Row4 Col4",
                        }
                    })
            });
                 
            InitializeComponent();
 
            this.ParentGrid.Columns[1].SortingState = Telerik.Windows.Controls.SortingState.Descending;
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.ParentGrid.ItemsSource = this._data;
        }
    }
 
    public class ParentData
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public string Col4 { get; set; }
 
        private List<ChildData> _childData;
        public List<ChildData> ChildData
        {
            get
            {
                return this._childData;
            }
            set
            {
                this._childData = value;
            }
         }
    }
 
    public class ChildData
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public string Col4 { get; set; }
    }
}

Thanks,
Chris

4 Answers, 1 is accepted

Sort by
0
Accepted
Yavor Georgiev
Telerik team
answered on 21 Jul 2010, 09:42 AM
Hi Chris,

 This is by design - when you change the ItemsSource, there is no guarantee that the data will be the same and that the SortDescriptors will apply. If you want to change the data, assign an ObservableCollection to the ItemsSource and then clear it out and add new items when you wish to change the data. This way the SortDescriptors will be preserved.

Regards,
Yavor Georgiev
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
Chris
Top achievements
Rank 1
answered on 22 Jul 2010, 09:28 PM
Thanks, that makes sense.
0
Rick Glos
Top achievements
Rank 1
answered on 25 Aug 2010, 11:10 PM
This is happening to me as well now with the update to the new controls.  Thanks for the explanation.
0
Rick Glos
Top achievements
Rank 1
answered on 26 Aug 2010, 01:30 AM
In case someone else needs this, I was binding my grid to a List because I was creating the list of objects at run time.  This is because our application allows users to build a grid from a selection of hundreds of column variations.  Anyway here's the bit of code I needed to make it an ObservableCollection

Old

IList genericRunTimeList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(dynamicRow));

New

// Put the DynamicRowType objects in an ObserveableCollection
Type oc = typeof(ObservableCollection<>);
oc = oc.MakeGenericType(new[] { dynamicRow });
IList genericRunTimeList = (IList)Activator.CreateInstance(oc);

Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Chris
Top achievements
Rank 1
Rick Glos
Top achievements
Rank 1
Share this question
or