Skip to main content

DevExpress v24.1 Update — Your Feedback Matters

Our What's New in v24.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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

  • 6 minutes to read

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.

#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 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:

using DevExpress.Xpo;

[Persistent("Person.Person")]
public class Person : XPLiteObject {
    public Person(Session session) : base(session) { }
    [Key, DevExpress.Xpo.DisplayName("ID")]
    public System.Int32 BusinessEntityID;        
    public string Title;
    [DevExpress.Xpo.DisplayName("First Name")]
    public string FirstName;
    [DevExpress.Xpo.DisplayName("Middle Name")]
    public string MiddleName;
    [DevExpress.Xpo.DisplayName("Last Name")]
    public string LastName;
}

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.

#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:

    ServerModeEx_XPServerCollSource_SetObjectClassInfo

  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.

    ServerModeEx_GridControl_PopulatingColumns

  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):

    using DevExpress.Xpo;
    using DevExpress.Xpo.DB;
    
    public partial class Form1 : Form {
        public Form1() {
            // Generate the connection string to the AdventureWorks database on local SQL Server.
            XpoDefault.ConnectionString = 
              MSSqlConnectionProvider.GetConnectionString("(local)", "AdventureWorks");
            InitializeComponent();            
        }
    }
    
  11. Run the project. The form with Grid Control filled with data will appear:

    ServerModeEx_GridControl_Result

#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):

using System.Windows.Forms;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Metadata;
using DevExpress.XtraGrid;

namespace ServerModeDemoRuntime {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            // Generate the connection string to the AdventureWorks database on local SQL Server.
            XpoDefault.ConnectionString = 
              MSSqlConnectionProvider.GetConnectionString("(local)", "AdventureWorks");
            // Create a Session object.
            Session session1 = new Session();
            // Create an XPClassInfo object corresponding to the Person class.
            XPClassInfo classInfo = session1.GetClassInfo(typeof(Person));
            // Create an XPServerCollectionSource object.
            XPServerCollectionSource xpServerCollectionSource1 = 
              new XPServerCollectionSource(session1, classInfo);
            // Create a grid control.
            GridControl gridControl1 = new GridControl();
            gridControl1.Dock = DockStyle.Fill;
            this.Controls.Add(gridControl1);
            // Bind the grid control to the data source.
            gridControl1.DataSource = xpServerCollectionSource1;
        }
    }

    [Persistent("Person.Person")]
    public class Person : XPLiteObject {
        public Person(Session session) : base(session) { }
        [Key, DevExpress.Xpo.DisplayName("ID")]
        public System.Int32 BusinessEntityID;
        public string Title;
        [DevExpress.Xpo.DisplayName("First Name")]
        public string FirstName;
        [DevExpress.Xpo.DisplayName("Middle Name")]
        public string MiddleName;
        [DevExpress.Xpo.DisplayName("Last Name")]
        public string LastName;
    }
}