HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Sunday, July 22, 2012

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 a server mode. For general information and theoretical aspects of binding controls in a server mode, please refer to the Server Mode section.

Prerequisites

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

In a 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 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_Contact). For general information on creating persistent objects, see Basics of Creating Persistent Objects for Existing Data Tables. The Person_Contact class is declared as follows:

C#

using DevExpress.Xpo;

 

[Persistent("Person.Contact")]

public class Person_Contact : XPLiteObject {

    public Person_Contact(Session session) : base(session) { }

    [Key, DevExpress.Xpo.DisplayName("ID")]

    public System.Int32 ContactID;       

    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;

    [DevExpress.Xpo.DisplayName("E-mail")]

    public string EmailAddress;       

    public string Phone;

}

VB

Imports DevExpress.Xpo;
 
<Persistent("Person.Contact")> _
Public Class Person_Contact
    Inherits XPLiteObject
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub 
    <Key(), DevExpress.Xpo.DisplayName("ID")> _
        Public ContactID As System.Int32
    Public Title As String 
    <DevExpress.Xpo.DisplayName("First Name")> _
    Public FirstName As String 
    <DevExpress.Xpo.DisplayName("Middle Name")> _
    Public MiddleName As String 
    <DevExpress.Xpo.DisplayName("Last Name")> _
    Public LastName As String 
    <DevExpress.Xpo.DisplayName("E-mail")> _
    Public EmailAddress As String 
    Public Phone As String 
End Class 

The Person_Contact 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_Contact class to the "Person.Contact" 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_Contact 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_Contact' item. The class' name will be preceded by the namespace's name:

  1. Set the XPServerCollectionSource.Session property to the session object created before.
  2. Add a GridControl control from the Toolbox to the form.
  3. 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_Contact class.

  1. 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):
C#
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();            
    }
}
VB
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
 
Public Class Form1
    Sub New()
        ' Generate the connection string to the AdventureWorks database on local SQL Server. 
        XpoDefault.ConnectionString = _
          MSSqlConnectionProvider.GetConnectionString("(local)", "AdventureWorks")
        InitializeComponent()
    End Sub 
End Class 
  1. Run the project. The form with GridControl filled with data will appear:

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 XtraGrid and eXpress Persistent Objects (DevExpress.Data, DevExpress.Utils, DevExpress.Xpo, DevExpress.XtraEditors and DevExpress.XtraGrid):

C#
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_Contact class. 
            XPClassInfo classInfo = session1.GetClassInfo(typeof(Person_Contact));
            // 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);
            // Enable server mode. 
            gridControl1.ServerMode = true;
            // Bind the grid control to the data source. 
            gridControl1.DataSource = xpServerCollectionSource1;
        }
    }
 
    [Persistent("Person.Contact")]
    public class Person_Contact : XPLiteObject {
        public Person_Contact(Session session) : base(session) { }
        [Key, DevExpress.Xpo.DisplayName("ID")]
        public System.Int32 ContactID;
        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;
        [DevExpress.Xpo.DisplayName("E-mail")]
        public string EmailAddress;
        public string Phone;
    }
}
VB
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Imports DevExpress.Xpo.Metadata
Imports DevExpress.XtraGrid
 
Public Class Form1
    Sub New()
        InitializeComponent()
        ' Generate the connection string to the AdventureWorks database on local SQL Server. 
        XpoDefault.ConnectionString = _
          MSSqlConnectionProvider.GetConnectionString("(local)", "AdventureWorks")
        ' Create a Session object. 
        Dim session1 As Session = New Session()
        ' Create an XPClassInfo object corresponding to the Person_Contact class. 
        Dim classInfo As XPClassInfo = session1.GetClassInfo(GetType(Person_Contact))
        ' Create an XPServerCollectionSource object. 
        Dim xpServerCollectionSource1 As XPServerCollectionSource = _
          New XPServerCollectionSource(session1, classInfo)
        ' Create a grid control. 
        Dim gridControl1 As GridControl = New GridControl()
        gridControl1.Dock = DockStyle.Fill
        Me.Controls.Add(gridControl1)
        ' Enable server mode. 
        gridControl1.ServerMode = True 
        ' Bind the grid control to the data source. 
        gridControl1.DataSource = xpServerCollectionSource1
    End Sub 
End Class 
 
<Persistent("Person.Contact")> _
Public Class Person_Contact
    Inherits XPLiteObject
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub 
    <Key(), DevExpress.Xpo.DisplayName("ID")> _
        Public ContactID As System.Int32
    Public Title As String 
    <DevExpress.Xpo.DisplayName("First Name")> _
    Public FirstName As String 
    <DevExpress.Xpo.DisplayName("Middle Name")> _
    Public MiddleName As String 
    <DevExpress.Xpo.DisplayName("Last Name")> _
    Public LastName As String 
    <DevExpress.Xpo.DisplayName("E-mail")> _
    Public EmailAddress As String 
    Public Phone As String 
End Class 

 

 

No comments:

Post a Comment