原創(chuàng)|其它|編輯:郝浩|2012-10-19 17:44:52.000|閱讀 1279 次
概述:本文將詳細為你介紹如何將DevExpress XtraGrid.GridControl控件綁定到服務(wù)器模式下的數(shù)據(jù)庫中。在本示例中,將一個grid網(wǎng)格控件綁定到AdventureWorks SQL數(shù)據(jù)庫的“Person.Contact”數(shù)據(jù)表。要按照下面的步驟,你需要訪問包含在此數(shù)據(jù)庫中的SQL Server實例。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
本文將詳細為你介紹如何將DevExpress XtraGrid.GridControl控件綁定到服務(wù)器模式下的數(shù)據(jù)庫中。
準(zhǔn)備工作
在本示例中,將一個grid網(wǎng)格控件綁定到AdventureWorks SQL數(shù)據(jù)庫的“Person.Contact”數(shù)據(jù)表。要按照下面的步驟,你需要訪問包含在此數(shù)據(jù)庫中的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
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:DevExpress中文網(wǎng)