下面的示例演示了如何在运行时刻创建并定制 GridLookUpEdit 控件。
在本示例中,使用一个“查找编辑器”编辑“Order Details”表 (NWind 数据库) 中“ProductID”字段的值。 它必须在下拉列表中显示可用产品的清单,产品存储在 NWind 数据库的“Products”表中。 通过从下拉列表选中一个行,最终用户就能更改当前订单的产品。 也创建了一个数据导航器,帮助最终用户在“Order Details”中导航。
要实现所需的功能,则必须设置“查找编辑器”的下列关键属性:
- 通过 DataBindings 属性,把编辑器的 BaseEdit.EditValue 属性绑定到“Order Details”表中的“ProductID”字段。
- 把编辑器的 RepositoryItemLookUpEditBase.DataSource 属性值设置为一个 DataView 对象,此对象包含 NWind 数据库的“Products”表中的数据行。
- 把编辑器的 RepositoryItemLookUpEditBase.ValueMember 属性值设置为“Products”表中的“ProductID”字段。 此字段的取值必须与编辑器的编辑值相匹配,因而与“Order Details”表中的“ProductID”字段匹配。
- 把编辑器的 RepositoryItemLookUpEditBase.DisplayMember 属性值设置为“Products”表中的“ProductName”字段。 这样标识 DataSource 中取值与编辑器显示文本相匹配的字段。
- 通过 ColumnView.Columns 属性,在下层视图中创建两个列。 这些列将在下拉数据源中显示“ProductID”和“ProductName”字段值。
本示例的运行结果在下面的插图中显示:
C# | 复制代码 |
---|---|
using DevExpress.XtraEditors; using DevExpress.XtraGrid.Columns; using System.Data.OleDb; // A lookup editor created at runtime. GridLookUpEdit gridLookup; // A navigator control to navigate the "Order Details" table. DataNavigator dataNav; // DataView for the "Order Details" table. DataView dvMain; // DataView for the "Products" table. DataView dvDropDown; //... private void Form1_Load(object sender, System.EventArgs e) { gridLookup = new GridLookUpEdit(); gridLookup.Bounds = new Rectangle(10, 40, 200, 20); this.Controls.Add(gridLookup); dataNav = new DataNavigator(); dataNav.Bounds = new Rectangle(10, 10, 250, 20); this.Controls.Add(dataNav); InitData(); InitLookUp(); dataNav.DataSource = dvMain; } private void InitData() { // Dataset to provide data from the database DataSet ds = new DataSet(); string connestionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\DB\\nwind.mdb"; // Connect to the "Order Details" table System.Data.OleDb.OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT * FROM [Order Details]", connestionString); // Load data from the "Order Details" table to the dataset dbAdapter.Fill(ds, "Order Details"); // Connect to the "Products" table dbAdapter = new OleDbDataAdapter("SELECT * FROM Products", connestionString); // Load data from the "Products" table into the dataset dbAdapter.Fill(ds, "Products"); DataViewManager dvm = new DataViewManager(ds); dvMain = dvm.CreateDataView(ds.Tables["Order Details"]); dvDropDown = dvm.CreateDataView(ds.Tables["Products"]); } private void InitLookUp() { // Bind the edit value to the ProductID field of the "Order Details" table; // the edit value matches the value of the ValueMember field. gridLookup.DataBindings.Add("EditValue", dvMain, "ProductID"); // Prevent columns from being automatically created when a data source is assigned. gridLookup.Properties.View.OptionsBehavior.AutoPopulateColumns = false; // The data source for the dropdown rows gridLookup.Properties.DataSource = dvDropDown; // The field for the editor's display text. gridLookup.Properties.DisplayMember = "ProductName"; // The field matching the edit value. gridLookup.Properties.ValueMember = "ProductID"; // Add two columns in the dropdown: // A column to display the values of the ProductID field; GridColumn col1 = gridLookup.Properties.View.Columns.AddField("ProductID"); col1.VisibleIndex = 0; col1.Caption = "Product ID"; // A column to display the values of the ProductName field. GridColumn col2 = gridLookup.Properties.View.Columns.AddField("ProductName"); col2.VisibleIndex = 1; col2.Caption = "Product Name"; // Set column widths according to their contents. gridLookup.Properties.View.BestFitColumns(); // Specify the total dropdown width. gridLookup.Properties.PopupFormWidth = 300; } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraEditors Imports DevExpress.XtraGrid.Columns Imports System.Data.OleDb ' A lookup editor created at runtime. Dim gridLookup As GridLookUpEdit ' A navigator control to navigate the "Order Details" table. Dim dataNav As DataNavigator ' DataView for the "Order Details" table. Dim dvMain As DataView ' DataView for the "Products" table. Dim dvDropDown As DataView '... Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load gridLookup = New GridLookUpEdit() gridLookup.Bounds = New Rectangle(10, 40, 200, 20) Me.Controls.Add(gridLookup) dataNav = New DataNavigator() dataNav.Bounds = New Rectangle(10, 10, 250, 20) Me.Controls.Add(dataNav) InitData() InitLookUp() dataNav.DataSource = dvMain End Sub Private Sub InitData() ' Dataset to provide data from the database Dim ds As New DataSet() Dim connestionString As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DB\nwind.mdb" ' Connect to the "Order Details" table Dim dbAdapter As New OleDbDataAdapter("SELECT * FROM [Order Details]", connestionString) ' Load data from the "Order Details" table to the dataset dbAdapter.Fill(ds, "Order Details") ' Connect to the "Products" table dbAdapter = New OleDbDataAdapter("SELECT * FROM Products", connestionString) ' Load data from the "Products" table into the dataset dbAdapter.Fill(ds, "Products") Dim dvm As New DataViewManager(ds) dvMain = dvm.CreateDataView(ds.Tables("Order Details")) dvDropDown = dvm.CreateDataView(ds.Tables("Products")) End Sub Private Sub InitLookUp() ' Bind the edit value to the ProductID field of the "Order Details" table; ' the edit value matches the value of the ValueMember field. gridLookup.DataBindings.Add("EditValue", dvMain, "ProductID") ' Prevent columns from being automatically created when a data source is assigned. gridLookup.Properties.View.OptionsBehavior.AutoPopulateColumns = False ' The data source for the dropdown rows gridLookup.Properties.DataSource = dvDropDown ' The field for the editor's display text. gridLookup.Properties.DisplayMember = "ProductName" ' The field matching the edit value. gridLookup.Properties.ValueMember = "ProductID" ' Add two columns in the dropdown: ' A column to display the values of the ProductID field; Dim col1 As GridColumn = gridLookup.Properties.View.Columns.AddField("ProductID") col1.VisibleIndex = 0 col1.Caption = "Product ID" ' A column to display the values of the ProductName field. Dim col2 As GridColumn = gridLookup.Properties.View.Columns.AddField("ProductName") col2.VisibleIndex = 1 col2.Caption = "Product Name" ' Set column widths according to their contents. gridLookup.Properties.View.BestFitColumns() ' Specify the total dropdown width. gridLookup.Properties.PopupFormWidth = 300 End Sub |