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

How to implement CRUD operations using DXGrid and eXpress Persistent Objects

$
0
0

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

The test sample requires the SQL Express service and MSAccess to be installed on your machine.

We have created the XPOServerModeCRUDBehavior and XPOInstantModeCRUDBehavior attached behaviors for GridControl. For instance:

[XML]
<dxg:GridControl><i:Interaction.Behaviors><crud:XPOServerModeCRUDBehavior.../></i:Interaction.Behaviors></dxg:GridControl>

 

The XPServerModeCRUDBehavior and XPInstantModeCRUDBehavior classes contain 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:XPServerModeCRUDBehaviorNewRowForm="{StaticResource ResourceKey=EditRecordTemplate}"EditRowForm="{StaticResource ResourceKey=EditRecordTemplate}"/>

This Behavior classes requires the following information from your data model:

- XPObjectType - the type of rows;

- DataServiceContext - database entities;

- PrimaryKey - the primary key of the database table;

- CollectionSource/InstantCollectionSource - an object of the EntityServerModeDataSource type.

[XML]
<dxg:GridControl><i:Interaction.Behaviors><crud:XPOServerModeCRUDBehaviorXPObjectType="{x:Type local:Items}"CollectionSource="{Binding Collection}"PrimaryKey="Id"/></i:Interaction.Behaviors></dxg:GridControl>

The XPInstantModeCRUDBehavior class for SL contains the ServiceHelper property, which refers to an object that provides actions to work with databases

[C#]
helper.ServiceHelper=newServiceHelper(helper,newUri("http://localhost:54177/WcfDataService.svc/"));

See the XPInstantFeedbackSource and XPServerCollectionSource classes to learn more about XPInstantFeedbackSource and XPServerCollectionSource.

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:XPOServerModeCRUDBehaviorx: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 XPOServerModeCRUDBehavior and XPOInstantModeCRUDBehavior solution support 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.


To learn more on how to implement similar functionality in Silverlight, refer to the T245411 example.


Viewing all articles
Browse latest Browse all 7205

Trending Articles



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