原创|其它|编辑:郝浩|2012-10-19 17:44:52.000|阅读 1254 次
概述:本文将详细为你介绍如何将DevExpress XtraGrid.GridControl控件绑定到服务器模式下的数据库中。在本示例中,将一个grid网格控件绑定到AdventureWorks SQL数据库的“Person.Contact”数据表。要按照下面的步骤,你需要访问包含在此数据库中的SQL Server实例。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
本文将详细为你介绍如何将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 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 . 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 class (it could also be derived from the 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 .
The persistent object must always contain a public property corresponding to a key field. This property must be preceded by the attribute.
Note the Persistent keyword before the class name. This represents the attribute that is used to map the Person_Contact class to the "Person.Contact" data table. The 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:
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.
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:
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
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自:DevExpress中文网