2024年终活动

DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

DevExpress XtraGrid数据绑定示例二:服务器模式下绑定GridControl到数据库

来源:本站原创   发布时间:2012-10-10   浏览:6217次

本文将详细为你介绍如何将DevExpress XtraGrid.GridControl控件绑定到服务器模式下的数据库中。

准备工作

在本示例中,将一个grid网格控件绑定到AdventureWorks SQL数据库的“Person.Contact”数据表。要按照下面的步骤,你需要访问包含在此数据库中的SQL Server实例。

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


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;
}


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:

 grid控件, XtraGrid,GridControl,数据绑定,示例

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_Contact class.

 grid控件, XtraGrid,GridControl,数据绑定,示例

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

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


11、Run the project. The form with GridControl filled with data will appear:

grid控件, XtraGrid,GridControl,数据绑定,示例

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


本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/370.html
扫码咨询
电话咨询
023-68661681
返回
顶部