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
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.
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...
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.
I've updated your example to reproduce the behaviour. How can I submit the project to you?
Best regards,
Christian
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.
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);
}
}
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.
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.
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.
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
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.
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