[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [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
   [Expand]Get Started With Data Grid and Views
   [Expand]Data Binding
    Unbound Columns
   [Expand]Data Editing and Validation
   [Expand]Filter and Search
   [Expand]Focus and Selection Handling
    Format Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
   [Expand]Batch Modifications
    Hit Information
    Popup Menus
   [Expand]Save and Restore Layout
   [Expand]Visual Elements
   [Expand]Design-Time Features
    [Expand]Conditional Formatting
    [Collapse]Data Binding
      Examples: Data Binding
      How to: Add an Unbound Column Storing Arbitrary Data
      How to: Bind a GridControl to a Database in Server Mode
      How to: Create and customize a custom GridLookUpEdit control
      How to: Create and Customize a GridLookUpEdit Control at Runtime
      How to: Filter a LookUp(ComboBox) Column Based on Another Column Value
      How to: Reload Data from the Bound Database in Server Mode (XPO)
    [Expand]Data Editing
    [Expand]Data Presentation
    [Expand]Export and Printing
    [Expand]General Use
    [Expand]Layout View
    [Expand]Navigation and Selection
    [Expand]Skins and Look and Feel
    [Expand]Sorting and Grouping
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Gantt Control
  [Expand]Chart Control
  [Expand]Map Control
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Sunburst 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]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Bind a GridControl to a Database in Server Mode

This topic provides step-by-step instructions on how to bind a GridControl to a data source in server mode. For general information and theoretical aspects of binding controls in a server mode, please refer to the Large Data Sources: Server and Instant Feedback Modes section.

Expanded Prerequisites

In this example, a grid control will be bound to a "Person.Person" data table. This data table is available within the standard AdventureWorks2014 SQL database. To follow the steps below, you need to have access to an SQL Server instance that contains this database.

In server mode, regardless of whether a control will be bound to a data source at design time or runtime, you need to create an object that describes the target data table. This object should identify the data table's name, and required data fields to be shown as columns in the grid. The Server Mode: Binding to a Data Source Using eXpress Persistent Objects topic outlines two methods of creating such an object: 1) via a persistent object and 2) via a typed System.Data.DataTable object.

In this example, descriptive information on the target data table is provided by creating a persistent object class (Person). For general information on creating persistent objects, see Basics of Creating Persistent Objects for Existing Data Tables. The Person class is declared as follows:

The Person persistent object is derived from the XPLiteObject class (it could also be derived from the XPBaseObject class). It exposes public properties corresponding to data fields in the target data table, and a public constructor with a session parameter. This constructor is required to allow data to be handled within the scope of a non-default Session.

The persistent object must always contain a public property corresponding to a key field. This property must be preceded by the KeyAttribute attribute.

Note the Persistent keyword before the class name. This represents the PersistentAttribute attribute that is used to map the Person class to the "Person.Person" data table. The DisplayNameAttribute attribute allows custom display names to be associated with properties. These will be displayed within column headers in the grid control.

Expanded Design-Time Example

  1. Create a new Windows Application in Visual Studio.
  2. Add a Session and an XPServerCollectionSource components from the Toolbox to the form.
  3. Switch to the code editor and add the Person class declaration (see above).
  4. Re-build the project by selecting the Build | Rebuild Solution menu command.
  5. Switch to the design-time editor, and display properties of the created XPServerCollectionSource component in the Properties window.
  6. In the Properties window, open the dropdown in the cell corresponding to the XPServerCollectionSource.ObjectClassInfo property. Select the 'Person' item. The class' name will be preceded by the namespace's name:

  7. Set the XPServerCollectionSource.Session property to the session object created before.
  8. Add a GridControl control from the Toolbox to the form.
  9. In the Properties window, set the GridControl.DataSource property to the created XPServerCollectionSource object (xpServerCollectionSource1). After the data source has been assigned, the grid control automatically creates columns for all public properties exposed by the Person class.

  10. Switch to the code editor and in the form's constructor specify connection settings to the SQL Server instance that contains the AdventureWorks database. In this example, a connection string is provided via the static XpoDefault.ConnectionString property. The connection string specified by this property is used by default by all sessions. To generate a connection string for SQL Server, the static MSSqlConnectionProvider.GetConnectionString method can be used. The method's overload that is called below, takes the server and database names as parameters and generates the connection string using Windows integrated security (you can also use another overload to generate a connection string using a specific user name and password):

  11. Run the project. The form with Grid Control filled with data will appear:

Expanded Runtime Example

The following code is equivalent to design-time operations shown above. To compile this code, you need to manually add references to assemblies required by Grid Control and eXpress Persistent Objects (DevExpress.Data, DevExpress.Utils, DevExpress.Xpo, DevExpress.XtraEditors and DevExpress.XtraGrid):

Is this topic helpful?​​​​​​​