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

RadGridView with SortDescriptor removing wrong Row

10 Answers 125 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 26 Sep 2012, 01:47 PM
Hi,

I use your RadGridView (Version 2012.2.0607.40) in my application to display a grid with e.g. two columns named ColumnA and ColumnB.
The data of the grid is sorted by this SortDescriptor:

<telerik:RadGridView.SortDescriptors>
    <telerik:SortDescriptor Member="ColumnA"
                    SortDirection="Descending" />
</telerik:RadGridView.SortDescriptors>

The grid is bound to an ObservableCollection. Now I want to delete a row if the data of ColumnB of a row is changed to a specific value by the user. So I subscribed to the PropertyChanged Event of my row ViewModel and if the data of ColumnB is changed to the value I remove this row from my ObservableCollection. This works without a problem but the grid is not updated correctly. It removes the last row even if this row was not removed from my collection. Only if I manually reorder the list by clicking on the header of ColumnA, I get the correct data displayed.

Do you have any suggestion how to fix this problem?

Best regards,

Christian

10 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 26 Sep 2012, 01:50 PM
Hi,

 If CollectionChanged event of your collection is properly raised after the remove the grid will be updated as well. 

Kind regards,
Vlad
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Christian
Top achievements
Rank 1
answered on 26 Sep 2012, 01:54 PM
Hi Vlad,

I use the .Net ObservableCollection and the binding is done by this code:
<telerik:RadGridView ItemsSource="{Binding Rows}">

And if I remove the SortDescriptor the grid is updated properly. But with the SortDescriptor it removes the last row...
0
Vlad
Telerik team
answered on 26 Sep 2012, 02:00 PM
Hi,

 I've attached an example project using the specified version. Everything worked as expected - the grid is updated properly. 

Regards,
Vlad
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Christian
Top achievements
Rank 1
answered on 26 Sep 2012, 02:28 PM
Hi Vlad,

I've updated your example to reproduce the behaviour. How can I submit the project to you?

Best regards,

Christian
0
Vlad
Telerik team
answered on 26 Sep 2012, 02:31 PM
Hi,

If the code is not too much you can post it here. 

Greetings,
Vlad
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Christian
Top achievements
Rank 1
answered on 26 Sep 2012, 02:39 PM
Okay.

I've added a RowViewModel that implements INotifyPropertyChanged and has two Properties of Type short. After this I've added the WindowViewModel with only one Property:

private ObservableCollection<RowViewModel> _rows;
 
public ObservableCollection<RowViewModel> Rows
{
    get
    {
        if (_rows == null)
        {
            _rows = new ObservableCollection<RowViewModel>();
        }
 
        return _rows;
    }
    set
    {
        _rows = value;
    }
}

The constructor of the WindowViewModel:
public WindowViewModel()
{
    for (short i = 0; i < 10; i++)
    {
        RowViewModel row = new RowViewModel() { ColumnA = i, ColumnB = 1 };
        Rows.Add(row);
 
        row.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(row_PropertyChanged);
    }
}
And this PropertyChanged handler:
private void row_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "ColumnB":
        {
            RowViewModel row = sender as RowViewModel;
 
            if (row.ColumnB == 0)
            {
                Rows.Remove(row);
            }
 
            break;
        }
    }
}

The constructor of MainWindow is changed to:
public MainWindow()
 {
     InitializeComponent();
 
     //DataContext = new ObservableCollection<object>(from i in Enumerable.Range(0, 20) select new { ID = i });
     DataContext = new WindowViewModel();
 }

And the XAML of the Grid:
<telerik:RadGridView ItemsSource="{Binding Rows}"
                        Grid.Row="1"
                        AutoGenerateColumns="False">
       <telerik:RadGridView.SortDescriptors>
           <telerik:SortDescriptor Member="ColumnA" SortDirection="Descending" />
       </telerik:RadGridView.SortDescriptors>
       <telerik:RadGridView.Columns>
           <telerik:GridViewDataColumn Header="ColumnA"
                                       DataMemberBinding="{Binding ColumnA}"/>
           <telerik:GridViewDataColumn Header="ColumnB"
                                       DataMemberBinding="{Binding ColumnB}"
                                       ValidatesOnDataErrors="None" />
       </telerik:RadGridView.Columns>
   </telerik:RadGridView>

The ValidatesOnDataErrors="None" is needed because otherwhise I get a Exception.

Edit: If you now change the value of ColumnB of a row to 0, the row gets removed from the ObservableCollection but the grid is not updated correctly.
0
Accepted
Nedyalko Nikolov
Telerik team
answered on 27 Sep 2012, 08:16 AM
Hi,

The problem comes from the fact that when sorting is applied "Edit" operation actually is a combination of "Remove" and "Add" operations. RadGridView properly removes the item (based on remove collection change), but committing the "Edit" operation adds this item again to the source collection.
The issue is already fixed and will be available with the next internal build (Monday 1 Oct 2012).
However I would recommend using "filtering" feature of RadGridView for handling such scenarios, since listening for PropertyChange for every item in the collection could lead to performance issues.

Regards,
Nedyalko Nikolov
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Christian
Top achievements
Rank 1
answered on 27 Sep 2012, 08:23 AM
Hi,

thank you very much for this answer. I will try the next internal build. Howerver, could you tell me when the next offical release is, that contains the fix?

The PropertyChange was only to have a simple scenario to show the problem. Our real application is a little bit more complicated because it uses a Datatable and Dataview and when the Dataview get's changed we update the ObservableCollection.

Best Regards,
Christian
0
Hristo
Telerik team
answered on 27 Sep 2012, 08:52 AM
Hi,

Our Q3 2012 official release will be available in the second half of October.

Kind regards,
Hristo
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Christian
Top achievements
Rank 1
answered on 04 Oct 2012, 09:27 AM
Short Feedback:

Your changes are working as expected. I checked it with your Internal Build from Monday 1 Oct 2012.
Looking forward to your official Q3 release.

Best regards,

Christian
Tags
GridView
Asked by
Christian
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Christian
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Hristo
Telerik team
Share this question
or