Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Collapse]Data Grid
   [Collapse]Getting Started
    [Collapse]Walkthroughs
     [Expand]Appearance and Conditional Formatting
     [Expand]Data Editing
     [Expand]Hit Information
     [Collapse]Data Binding and Working with Columns
       Tutorial: ADO.NET Data
       Tutorial: Working with Columns in Code
       Tutorial: Unbound Columns
       Tutorial: Create and Manage Columns at Design Time
       Tutorial: Entity Framework Data
       Tutorial: Create and Manage Data in Code and Apply Data Annotation Attributes
       Tutorial: Large Data Sources and Instant Feedback with Server Mode
     [Expand]Grid View - Columns, Rows and Cells
     [Expand]Banded Views
     [Expand]WinExplorer View
     [Expand]Tile View
     [Expand]Grouping
     [Expand]Filter and Search
     [Expand]Sorting
     [Expand]Summaries
     [Expand]Split Presentation
     [Expand]Row Preview Sections
     Data Grid and Views
   [Expand]Data Binding
   [Expand]Views
   [Expand]Data Editing and Validation
   [Expand]Grouping
   [Expand]Sorting
   [Expand]Filter and Search
   [Expand]Summaries
   [Expand]Focus and Selection Handling
    Formatting Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
    Scrolling
   [Expand]Batch Modifications
   [Expand]Hit Information
    Hints
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]Examples
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Tutorial: Entity Framework Data

This walkthrough is a transcript of the Entity Framework Data video available on the DevExpress YouTube Channel.

In this tutorial, you'll learn how to bind a grid control to an Entity Framework data source, how to use Data Annotation Attributes to change the way data is presented and managed by the grid, and how to post cell value changes back to the data source.

Expanded Starting Point

Start with a project that has a Windows Form and an empty GridControl on it. The solution also includes an Entity Framework model named Model1 by default. You can double-click it to see its visual representation on the diagram. For this tutorial, use a modified version of the AdventureWorks database. It contains only one table with just a few fields in it.

For information on creating and modifying Entity Framework models, please refer to the Microsoft's Entity Framework Documentation.

Expanded Binding to Entity Framework Data Source

The easiest way to bind the Entity Framework model to the grid control is by using the Data Source Configuration Wizard. Invoke the grid control's smart tag and click the Data Source Wizard.

Choose the Entity Framework technology and select the existing data connection or create a new one. In this tutorial, use the existing connection to the sample Microsoft AdventureWorksDW2008R2 database. Click Next to proceed.

On the next page, you will be asked to choose the desired binding mode. Choose the Binding using Binding Source component option and click Next.

Finally, select the table to be shown within your grid control.

Expanded Result of Binding to Entity Framework Data

The grid control is now bound to the EF data and all the required code is already generated. You can see the automatically generated code in the .CS file. Now run the app to see the result.

Here are some observations about how grid displays its bound data.

  • Simple numeric formatting is applied to the Price field, but it would make more sense to use Currency formatting.
  • The Description field contains lengthy values, so it will be hard to edit them inside grid cells.
  • The grid is smart enough to assign a special editor to columns bound to date-time fields.
  • Column names are simply derived from field names.
  • There are obviously no data validation rules applied to the grid.

Expanded Applying Data Annotation Attributes

All that default behavior can be changed using the GridControl settings. One more way to do this is by changing data field attributes or Data Annotation Attributes. This may come in handy when you have multiple controls bound to the same data source. This way, you don't have to set up the same rules. See how this works.

Open the DimProducts.cs file that contains the data model for your table. Here you can see all available data fields, declared as properties of the DimProduct class.

Add data attributes to these properties and see how this changes grid behavior.

Note

Note that your application should reference the System.ComponentModel.DataAnnotations library to do so.

  • For the EnglishProductName field, add the Required attribute. This will not permit end-users to set empty values to this column's cells.

    Run the application to make sure that empty values are no longer allowed.

  • For the DealerPrice field, add the DataType attribute to indicate that these integer values should be treated as currency data. Additionally, apply the Range attribute to set the minimum and maximum allowed values.

  • For the EnglishDescription field, use the DataType attribute to change the data type from simple to multiline text. The Display attribute will specify the Name and Description for the column.

    Run the application to see updated captions used for the column header and its tooltip. Also, notice that cells now use the MemoEdit, since you specified the data type to be multiline text.

  • Finally, for the StartDate and EndDate columns, apply different formatting and editing rules. The first date will be treated as a simple text value. The second column's data will remain as a date-time object, but the formatting will change to display the full month, name and year.

    Launch the app and see that no special editor is now displayed in the StartDate column. The EndDate column kept the editor, but the formatting has changed.

To learn more about all existing data attributes, see the this MSDN article.

Expanded Posting Data Back to the Data Source

To make sure that cell value changes in the grid control are posted back to the data source, you need to add a few lines of code. Save the changes when the grid raises the ColumnView.RowUpdated event. You need access the data source context, thus you must change the variable's scope so it's available to all methods in the Form class. In the handler, all you need to do is call the SaveChanges method.

Now run the application and change the value in the Dealer Price column. Move focus to another row to ensure that the ColumnView.RowUpdated event is raised. Then, close the application and run it again. You will see that the value has been saved.

Expanded See Also

How would you rate this topic?​​​​​​​