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

How to implement CRUD operations using DXGrid and LINQ to SQL Classes

$
0
0

This example shows how to use LinqInstantFeedbackDataSource or LinqServerModeDataSource 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 LINQServerModeCRUDBehavior and LINQInstantModeCRUDBehavior attached behaviors for GridControl. For instance:

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

The LINQServerModeCRUDBehavior and LINQInstantModeCRUDBehavior 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:LINQServerModeCRUDBehaviorNewRowForm="{StaticResource ResourceKey=EditRecordTemplate}"EditRowForm="{StaticResource ResourceKey=EditRecordTemplate}"/>

These Behavior classes require the following information from your data model:

- RowType - the type of rows;

- DataContext - database entities;

- DataSource - an object of the LinqInstantFeedbackDataSource or LinqServerModeDataSource type.

[XML]
<dxg:GridControl><i:Interaction.Behaviors><crud:LINQServerModeCRUDBehaviorRowType="{x:Type local:Item}"DataContext="{Binding Source={StaticResource DataClassesDataContext}}"><crud:LINQServerModeCRUDBehavior.DataSource><dxsm:LinqServerModeDataSourceKeyExpression="Id"QueryableSource="{Binding Items, Source={StaticResource DataClassesDataContext}}"/></crud:LINQServerModeCRUDBehavior.DataSource></crud:LINQServerModeCRUDBehavior></i:Interaction.Behaviors></dxg:GridControl>

See the LinqServerModeDataSource and LinqInstantFeedbackDataSource classes to learn more about LinqServerModeDataSource and LinqInstantFeedbackDataSource .

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:LINQServerModeCRUDBehaviorx: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 LINQServerModeCRUDBehavior and LINQInstantModeCRUDBehavior solutions 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.

Example Comments

Added By: dua ali at: 7/2/2013 6:04:56 PM    

Are you using silverlight?


Viewing all articles
Browse latest Browse all 7205

Trending Articles



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