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

Using EF 4.0 - can add new row *or* sort

8 Answers 120 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lynne
Top achievements
Rank 1
Lynne asked on 26 Jul 2011, 05:12 AM
I'm using Entity Framework 4.0 and have a "Players" class.  Bone-stock, nothing added or changed, just as VS2010 generated it, which means it's an ObjectSet.

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

Sort by
0
Maya
Telerik team
answered on 26 Jul 2011, 03:40 PM
Hello Lynne,

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!

0
Lynne
Top achievements
Rank 1
answered on 26 Jul 2011, 04:57 PM

  • 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?

0
Lynne
Top achievements
Rank 1
answered on 29 Jul 2011, 10:31 PM
Anyone have any ideas?  

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.
0
Maya
Telerik team
answered on 01 Aug 2011, 12:14 PM
Hi Lynne,

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.
 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Lynne
Top achievements
Rank 1
answered on 03 Aug 2011, 10:27 PM
Your solution might work, but it's not helpful because the RadGridview control in it isn't the same as the one I'm using.  The control in your project, for instance, has the properties DataContext, Style, and IsEnabled under the Common grouping.  It doesn't even have a group called Columns.

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" />
0
Maya
Telerik team
answered on 05 Aug 2011, 09:30 AM
Hi Lynne,

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 ? 

 

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Lynne
Top achievements
Rank 1
answered on 08 Aug 2011, 10:33 PM
My previous reply seems pretty clear.  The example you've provided uses a different (much older, apparently) grid than the one you're selling, and that I'm using.  This means your example doesn't help me to understand what I'm doing wrong.  Would you please post an example using the currently shipping product?  

Thanks.
0
Vlad
Telerik team
answered on 10 Aug 2011, 09:14 AM
Hi,

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!

Greetings,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
GridView
Asked by
Lynne
Top achievements
Rank 1
Answers by
Maya
Telerik team
Lynne
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or