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

Very simple example application with a RadGridView with Entity Framework and MVVM

12 Answers 807 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Uwe
Top achievements
Rank 1
Uwe asked on 16 Sep 2012, 02:23 PM

Hello,
can anyone tell me where I can find a (very) simple example application with a RadGridView with select, insert, update and delete with the Entity Framework and maybe in an MVVM Environment.


Greetings Uwe

12 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 17 Sep 2012, 05:36 AM
Hi,

 Have you tried our demos for RadEntityFrameworkDataSource?

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
Uwe
Top achievements
Rank 1
answered on 21 Sep 2012, 08:16 AM

Hello, thank you very much for the reply. I did not notice the Lode and Submit Button in the EntityFrameworkDataSource.WPF project :-|.

But how can I bind the RadEntityFrameworkDataSource in my ViewModel and in general, all the events which are mentioned in the tutorial and the documentation, how do I fetch them in the VM? For example the CellValidating event?

Best regards

0
Dimitrina
Telerik team
answered on 26 Sep 2012, 08:34 AM
Hello,

You could bind the GridView to a collection (for example CustomersView) defined in the ViewModel as follows:
public class MyViewModel
{
    private readonly QueryableEntityCollectionView<Customer> customersView;
    private readonly NorthwindEntities objectContext;
 
    public MyViewModel()
        {
        this.objectContext = new NorthwindEntities();
            this.customersView = new QueryableEntityCollectionView<Customer>(objectContext, "Customers");
    }
 
    public QueryableEntityCollectionView<Customer> CustomersView
    {
        get { return this.customersView; }
    }
}
<telerik:RadGridView Name="customersGrid"...
                             ItemsSource="{Binding CustomersView}"/>
 

As to your second question, the CellValidating event is an UI event and you should not work with it in the ViewModel. You could instead implement the INotifyPropertyChanged interface (for notifications) and the IDataErrorInfo interface (for validation).


Regards,
Didie
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
Uwe
Top achievements
Rank 1
answered on 28 Sep 2012, 02:08 PM

Hello Didie, thank you for your replay, but in my .xaml I have this:

<telerik:RadEntityFrameworkDataSource Name="SuppliersEntityFrameworkDataSource" QueryName="Suppliers"/>
<telerik:RadGridView ItemsSource="{Binding DataView, ElementName=SuppliersEntityFrameworkDataSource}"  IsEnabled="{Binding IsChecked, ElementName=UnLock, Mode=TwoWay}" />
<telerik:RadDataPager Source="{Binding DataView, ElementName=SuppliersEntityFrameworkDataSource}" PageSize="17" />

In my code behind constructor I have this:

ObjectContext OC = (ObjectContext)MSSQLAccess.MSDataAccess.GetOBContext();
SuppliersEntityFrameworkDataSource.ObjectContext = OC;

But I want to have access to SuppliersEntityFrameworkDataSource  in my VM. 

0
Rossen Hristov
Telerik team
answered on 28 Sep 2012, 02:18 PM
Hello,

That is the whole point.

You asked for an MVVM example. You can't place a visual control in a view model (code-behind).

That is why you should work with the QueryableEntityCollectionView<T> instead. Forget about the control. In fact the control RadEntityFrameworkDataSource is a very thin wrapper over this QueryableEntityCollectionView<T> class. All the work is actually done by QueryableEntityCollectionView<T>. That is why we made it public -- so you can place it in your view model and do MVVM.

So forget about the control. Please do as my colleague suggested. Expose this collection as a property on your view model and then bind both the grid and the pager directly to it.

Regards,
Ross
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
Rossen Hristov
Telerik team
answered on 28 Sep 2012, 02:24 PM
Hi,

Follow up: In case you do not need to perform any editing, i.e. your grid is read only.

You can simply expose ObjectContext.Suppliers as a property on your view model and then bind RadGridView and RadDataPager to it. Since ObjectContext.Suppliers is IQueryable RadGridView will be able to filter (Where), sort (OrderBy) and group (GroupBy) directly on the database server. RadDataPager will also do server-side paging (Skip and Take).

You can try this as well comletely eliminating the need for our control.

Kind regards,
Ross
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
Uwe
Top achievements
Rank 1
answered on 28 Sep 2012, 02:45 PM

Hello Ross, thank’s for helping. 
Did I unstained you right: the RadEntityFrameworkDataSource is not suitable for MVVM?
Regards Uwe

0
Dimitrina
Telerik team
answered on 29 Sep 2012, 10:34 AM
Hi,

 Yes, that is right, it is a visual control. You should use the suggested MVVM example.

Kind regards,
Didie
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
Uwe
Top achievements
Rank 1
answered on 29 Sep 2012, 03:40 PM

… ok, now I made the following: 

In xaml:

<telerik:RadGridView Name="MyGridView" Margin="29,89,38,70" ItemsSource="{Binding PagedSource, ElementName=Pager}" >
    <i:Interaction.Triggers>
            <i:InvokeCommandAction Command="{Binding SubmitChanges}"/>
    </i:EventTrigger>
    <i:EventTrigger EventName="RowLoaded" >
            <i:InvokeCommandAction Command="{Binding RowLoaded}" CommandParameter="{Binding ElementName=MyGridView}"/>
    </i:EventTrigger>
    </i:Interaction.Triggers>
</telerik:RadGridView>
<telerik:RadDataPager Name="Pager" Source="{Binding Suppliers}" PageSize="17" Grid.ColumnSpan="2" Margin="29,0,38,42" VerticalAlignment="Bottom" d:LayoutOverrides="Height"/>

In the ViewModel:


private readonly ObjectContext _oC = (ObjectContext)MSSQLAccess.MSDataAccess.GetOBContext();
public ObjectContext OC
{
    get { return this._oC; }
}
private readonly QueryableEntityCollectionView<MSSQLAccess.Supplier> _suppliers;
public QueryableEntityCollectionView<MSSQLAccess.Supplier> Suppliers
{
    get { return this._suppliers; }
}
 
/// <summary>
/// Constructor
/// </summary>
public CurrentDataViewModel()
{
    _oC = new MSSQLAccess.APOSEntities(MSSQLAccess.MSDataAccess.EntityBuilder.ToString());
    _suppliers = new QueryableEntityCollectionView<MSSQLAccess.Supplier>(_oC,"Suppliers");
}
 
...
 
/// <summary>
/// SubmitChanges Command Function
/// </summary>
public void SubmitChangesCF()
{
    OC.SaveChanges();
}

The data will be displayed, I can edit it in the RadGridView and they are stored back into the DB (via the SubmitChangesCF function). Everything works fine so far. 
But I see the hole table (Suppliers) in the GridView, is it possible to fill the GridView with a select statement, something like:

IQueryable<MSSQLAccess.Supplier IQsu;
IQsu = ((MSSQLAccess.APOSEntities)OC).Suppliers.Where(o => o.ShippingWeight == 10);
List<MSSQLAccess.Supplier> Su = IQsu.ToList<MSSQLAccess.Supplier>();


Please can you give me a hint
a) if this is possible and how to do it 
b) reload the data (from the program) into the GridView, for example if the data has changed in the meantime in the DB

Or is there a complete better way to do what I want?

Kind Regarts



0
Uwe
Top achievements
Rank 1
answered on 30 Sep 2012, 12:06 PM
to b)
I found it out by myself:

((QueryableEntityCollectionView<MSSQLAccess.Supplier>)Suppliers).Refresh(); 
0
Uwe
Top achievements
Rank 1
answered on 01 Oct 2012, 02:09 PM
to a)
setting a filter seems to be a possibility:

DateTime ToDay = DateTime.Now.Date;
 
GridViewColumn IdColumn = _theGridView.Columns["InsertDate"];
 
IColumnFilterDescriptor idFilter = IdColumn.ColumnFilterDescriptor;
 
idFilter.SuspendNotifications();
 
idFilter.FieldFilter.Filter1.Operator = Telerik.Windows.Data.FilterOperator.IsGreaterThanOrEqualTo;
idFilter.FieldFilter.Filter1.Value = ToDay;
 
idFilter.ResumeNotifications();

0
Rossen Hristov
Telerik team
answered on 01 Oct 2012, 02:41 PM
Hello,

Filtering, sorting, grouping or manually calling Refresh will all refresh the current view.

Here you can learn all about filtering. You can "simulate" everything an end-user can do through the UI by using IColumnFilterDescriptors. If you do not care about the UI, you can directly add FilterDescriptos to the QECV<T>.

All the best,
Ross
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Uwe
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Uwe
Top achievements
Rank 1
Dimitrina
Telerik team
Rossen Hristov
Telerik team
Share this question
or