If, in my _Loaded event I do this:
- CollectionViewSource myCollectionViewSource = (CollectionViewSource)this.Resources["playersViewSource"];
- myCollectionViewSource.Source = this.DbContext.Players;
I can click to add a new row, update existing items, etc. I can't click on row headers to sort them, though.
If I do this:
- myCollectionViewSource.Source = this.DbContext.Players.ToList();
then I can sort/and add a new row, but dbContext.SaveChanges() doesn't have any effect.
I'm new to both EF and the RadGridView, so I'm not sure what the problem is. I'd like to be able to both update and sort. How do I do this?
Thanks.
8 Answers, 1 is accepted
May you verify what is the result of the CanSort property of the View:
bool canSort = myCollectionViewSource.View.CanSort;
If it is false, you will not be able to sort from the RadGridView UI as well.
Considering the case, where you are calling the ToList() method and no changes have been saved, it would be the expected behavior as a new instance is created.
You may try to use an ObservableCollection<T> for example:
ObservableCollection<
Customer
> myCollectionViewSource = new ObservableCollection<
Customer
>(entities.Customers);
this.customersGrid.ItemsSource = myCollectionViewSource;
Kind regards,
Maya
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!
- myCollectionViewSource.View.CanSort = true
When using ObservableCollection, I can sort, and I can add new items to the grid, but the changes in the grid aren't reflected in the underlying dbContext entity, so that doing a dbContext.SaveChanges() doesn't have any effect.
How can I using binding and the Entity Framework-generated classes with a RadGridView so that I can sort and make changes to the grid in such as way that doing a dbContext.SaveChanges() on the bound datasource updates the database?
I'd like to use the stock Entity Framework objects as datasources, but it looks as though my choices boil down to either having sorting or updating.
I am sending you a sample project illustrating how you may both sort the grid as well as update its entries.
Please take a look at it and let me know whether this will suit your needs.
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
My RadGridView control has 23 properties and a lot of other groupings. I'd screenshot 'em but there doesn't seem to be a way to post them here.
I did try setting my .ItemsSource to my <entities.collection> (as in your example), and can sort each column once, before sorting stops working. And, even though the "click here to add new row" is showing, nothing happens when I click on it.
I've included the relevant references below.
Your .csproj references
<
Reference
Include
=
"Telerik.Windows.Controls, Version=2011.1.513.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL"
/>
<
Reference
Include
=
"Telerik.Windows.Controls.GridView, Version=2011.1.513.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL"
/>
<
Reference
Include
=
"Telerik.Windows.Controls.Input, Version=2011.1.513.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL"
/>
<
Reference
Include
=
"Telerik.Windows.Data, Version=2011.1.513.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL"
/>
My .csproj references
<
Reference
Include
=
"Telerik.Windows.Controls, Version=2011.2.712.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7"
/>
<
Reference
Include
=
"Telerik.Windows.Controls.GridView, Version=2011.2.712.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7"
/>
<
Reference
Include
=
"Telerik.Windows.Controls.Input, Version=2011.2.712.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7"
/>
<
Reference
Include
=
"Telerik.Windows.Data, Version=2011.2.712.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7"
/>
Unfortunately, I am not quite capable of grasping your requirements. Neither the DataContext, not any style or specific property is set in the project I previously attached. May you clarify what are the groupings that you are referring to ?
Considering insertion of a new item, it depends on whether the collection allows such action, not on whether you set the ShowInsertRow property of the grid.
Is it possible to modify the sample I sent so that it corresponds to your particular requirements or if possible to provide us with your own small application reproducing your scenario ?
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
Thanks.
Generally EF will create ObjectSet<TEntity> in the context however this is not UI friendly since it is not INotifyCollectionChanged, etc. The best way to achieve your goal is to wrap the original ObjectSet<TEntity> in something more convenient (for example ObservableCollection<TEntity>), listen for collection changes and transfer all changes to the data context.
You can find an example project attached - this will work as excepted no matter the grid version!
Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>