Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to implement CRUD operations using DXGrid and Entity Framework (Server Mode)

$
0
0

This example shows how to use EntityServerModeDataSource with DXGrid, and how to implement CRUD operations (e.g., add, remove, edit) in your application via special behavior.

Note that the test sample requires the SQL Express service to be installed on your machine.

We have created the EntityServerModeCRUDBehavior attached behavior for GridControl. For instance:

[XML]
<dxg:GridControl><dxmvvm:Interaction.Behaviors><crud:EntityServerModeCRUDBehavior...><crud:EntityServerModeCRUDBehavior.DataSource><dxsm:EntityServerModeDataSource.../></crud:EntityServerModeCRUDBehavior.DataSource></crud:EntityServerModeCRUDBehavior></dxmvvm:Interaction.Behaviors></dxg:GridControl>

The EntityServerModeCRUDBehavior class contains the NewRowForm and EditRowForm properties to provide the "Add Row" and "Edit Row" actions. With these properties, you can create the Add and Edit forms according to your requirements:

[XML]
<DataTemplatex:Key="EditRecordTemplate"><StackPanelMargin="8"MinWidth="200"><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><TextBlockText="ID:"VerticalAlignment="Center"Grid.Row="0"Grid.Column="0"Margin="0,0,6,4"/><dxe:TextEditx:Name="txtID"Grid.Row="0"Grid.Column="1"EditValue="{Binding Path=Id, Mode=TwoWay}"Margin="0,0,0,4"/><TextBlockText="Name:"VerticalAlignment="Center"Grid.Row="1"Grid.Column="0"Margin="0,0,6,4"/><dxe:TextEditx:Name="txtCompany"Grid.Row="1"Grid.Column="1"EditValue="{Binding Path=Name, Mode=TwoWay}"Margin="0,0,0,4"/></Grid></StackPanel></DataTemplate><crud:EntityServerModeCRUDBehaviorNewRowForm="{StaticResource ResourceKey=EditRecordTemplate}"EditRowForm="{StaticResource ResourceKey=EditRecordTemplate}"/>

This behavior requires the following information from your data model:

- EntityType - the type of rows;

- ObjectContext - database entities;

- DataSource - an object of the EntityServerModeDataSource type.

[XML]
<crud:EntityServerModeCRUDBehaviorEntityType="{x:Type local:Item}"ObjectContext="{StaticResource ResourceKey=DatabaseEntites}"><crud:EntityServerModeCRUDBehavior.DataSource><dxsm:EntityServerModeDataSourceKeyExpression="Id"QueryableSource="{Binding Items, Source={StaticResource DatabaseEntites}}"/></crud:EntityServerModeCRUDBehavior.DataSource></crud:EntityServerModeCRUDBehavior>

See the EntityServerModeDataSource Class to learn more about EntityServerModeDataSource.


Behavior class descendants support the following commands: NewRowCommand, RemoveRowCommand, EditRowCommand. You can bind your interaction controls with these commands with ease. For instance:

[XML]
<crud:EntityServerModeCRUDBehaviorx:Name="helper"/><StackPanelGrid.Row="1"Orientation="Horizontal"HorizontalAlignment="Center"><ButtonHeight="22"Width="60"Command="{Binding Path=NewRowCommand, ElementName=helper}">Add</Button><ButtonHeight="22"Width="60"Command="{Binding Path=RemoveRowCommand, ElementName=helper}"Margin="6,0,6,0">Remove</Button><ButtonHeight="22"Width="60"Command="{Binding Path=EditRowCommand, ElementName=helper}">Edit</Button></StackPanel>

By default, the EntityServerModeCRUDBehavior solution supports the following end-user interaction capabilities:

1. An end-user can edit selected row values by double-clicking on a grid row or by pressing the Enter key if the AllowKeyDownActions property is True.

2. An end-user can remove selected rows via the Delete key press if the AllowKeyDownActions property is True.

3. An end-user can add new rows, remove and edit them via the NewRowCommand, RemoveRowCommand, and EditRowCommand commands.

Question Comments

Added By: Claire N Streb at: 8/2/2012 6:38:47 AM    

There is a file in this example called Database.mdf that requires SQL Server version 661. My server only supports 655. Can you please provide instructions or more files so that I can run this example?

Added By: Haytham Z. at: 5/3/2015 7:28:42 AM    

Hi devexpress,
Thanks for the sample, but it doesn't work with EF 6. I tried using ((IObjectContextAdapter)my_context).ObjectContext in order to convert it to ObjectContext, but without success.
Any help please?

Added By: Ivan (DevExpress Support) at: 5/4/2015 2:05:03 AM    Hi Haytham,

I have extracted your question into a separate ticket - T237245. Let's continue our discussion there.

Added By: Mikhail Shubin at: 5/4/2015 2:33:32 AM    

Hi Ivan,
It seems the ticket T237245 is a private one. Please make it public.

Added By: Ivan (DevExpress Support) at: 5/4/2015 7:01:25 AM    

Hi Mikhail,
I have decided to create a new public ticket on your behalf and post the solution there - How to upgrade E3863 to make it work with EF6. Please refer to it for more information.

Added By: Pierre Tremblay at: 5/4/2015 3:42:52 PM    

I will also be interested to see the new ticket "How to upgrade E3863 to make it work with EF6".

Is there any plan to make that Behavior part of your MVVM framework?

Added By: Ivan (DevExpress Support) at: 5/5/2015 12:32:23 AM    

Hi Pierre,
We don't have immediate plans to add this class to our MVVM framework.
I have marked the T237435: How to upgrade E3863 to make it work with EF6 ticket public. Please refer to it.


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>