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

MVVM Binding RadGridView to EntityFrameworkDataSource

3 Answers 209 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Samuel
Top achievements
Rank 1
Samuel asked on 21 May 2015, 05:38 PM

I am using EF 6 to connect to a database in a WPF app using MVVM pattern.  I have a derived context like so:

 

public class TIMSContext : DbContext
  {
      public TIMSContext()
          : base("name=TIMSContext") { }
 
 
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          //map models...
      }
 
      public IQueryable<T> Query<T>()
      {
          return this.Set(typeof(T)).AsQueryable() as IQueryable<T>;
      }
 
  }

 

I want to use the EntityFrameworkDataSource to provide items to a RadGridView so that I don't have to write the paging/filtering code myself.  I have looked at the example, but I cannot get it to work.

 

My XAML:

<telerik:RadGridView x:Name="RadGridView1"
                               Grid.Row="0"
                               AlternateRowBackground="CornflowerBlue"
                               AlternationCount="2"
                               AutoGenerateColumns="True"
                               CanUserDeleteRows="False"
                               CanUserFreezeColumns="False"
                               CanUserInsertRows="False"
                               GroupRenderMode="Flat"
                               IsReadOnly="True"
                               ItemsSource="{Binding ShipmentData}"
                               RowIndicatorVisibility="Collapsed" />
 
          <telerik:RadDataPager Grid.Row="1"
                                DisplayMode="FirstLastPreviousNextNumeric"
                                PageSize="25"
                                Source="{Binding ShipmentData}"/>

 

Here is my Viewmodel:

public class ShipmentsViewModel :ViewModelBase
    {
        private readonly QueryableEntityCollectionView<TIMS.Data.Entities.ASN> shipments;
        private readonly TIMS.Data.TIMSContext ctx;
 
        public ShipmentsViewModel()
        {
            try
            {
                ctx = new Data.TIMSContext();
                shipments = new QueryableEntityCollectionView<Data.Entities.ASN>(ctx, "ASN");
                //Argument 1: cannot convert from 'TIMS.Data.TIMSContext' to 'System.Data.Objects.ObjectContext'
 
 
                //Also tried this which I saw on some forums...
                //shipments = new QueryableEntityCollectionView<Data.Entities.ASN>( ((IObjectContextAdapter)ctx).ObjectContext, "ASN");
                //Argument 1: cannot convert from 'System.Data.Entity.Core.Objects.ObjectContext' to 'System.Data.Objects.ObjectContext'
            
            }
            catch (Exception)
            {
            }
        }
 
        #region Properties
 
        public Object ShipmentData
        {
            get
            {
                return shipments;
            }
        }
         
        #endregion
}

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 22 May 2015, 12:38 PM
Hi,

There should not be problems populating RadGridView with data using EF6. You can check the RadEntityFrameworkDataSource examples in  WPF Demos for a reference.

Please note that in order to get RadEntityFrameworkDataSource working with EF6, you should reference Telerik.Windows.Controls.EntityFramework60 assembly.

Regards,
Dimitrina
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Samuel
Top achievements
Rank 1
answered on 22 May 2015, 07:22 PM

The demo doesn't work correctly.  It's missing the cast of the context to the IObjectContextAdapter.  However, I've gotten that piece resolved.  However, it seems the only way to do this is to pass in a sting ("ASN" in the code above) with the property name of the DbSet in the context.  But as you can see from my implementation above, I don't have explicitly defined properties.  Is there a way to do this without defining them?

 

 

0
Dimitrina
Telerik team
answered on 25 May 2015, 11:41 AM
Hello,

As it turns out I am not sure if this is the only way to resolve it.  Would it be possible for you to illustrate the specific case in a demo project with your exact setup and send it to us via a new support ticket? That way we may debug the solution with the source code directly and advise further.

Regards,
Dimitrina
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Samuel
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Samuel
Top achievements
Rank 1
Share this question
or