DevExpress_常用控件10_GridLookUpEdit

2.2 GridLookUpEdit

2.1.2

demo1效果如下:

DevExpress_常用控件10_GridLookUpEdit


demo1步骤如下:

1. 拖一个GridLookupEdit放到窗口中

2. 点击右侧小箭头,弹出菜单,并选择Design View

DevExpress_常用控件10_GridLookUpEdit

3. AddColumns添加三列(ID,Age,Name)

4. 分别设置显示的Caption和字段来源FieldName

DevExpress_常用控件10_GridLookUpEdit

5. 写代码如下


demo1代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace DXApplication_1
{
	public partial class GridLookupEditForm : DevExpress.XtraEditors.XtraForm
	{
		public GridLookupEditForm()
		{
			InitializeComponent();
		}

		public SqlConnection getConnection()
		{
			//连接本地数据库 server=localhost
			string connStr = @"server=.;database=db5;Integrated Security=true;";
			SqlConnection conn = new SqlConnection(connStr);
			return conn;
		}

		DataTable tmpTable = new DataTable();
		public DataTable getDataTable(string sqlStr)
		{
			SqlConnection sqlConn = this.getConnection();
			SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, sqlConn);
			adapter.Fill(tmpTable);
			sqlConn.Close();
			sqlConn.Dispose();
			return tmpTable;
		}


		private void GridLookupEditForm_Load(object sender, EventArgs e)
		{
			// dataSource
			DataTable dataTable = getDataTable("select ID,Age,Name from Girl");
			// 
			gridLookUpEdit1.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
			gridLookUpEdit1.Properties.DataSource = dataTable;
			gridLookUpEdit1.Properties.DisplayMember = "ID";
			gridLookUpEdit1.Properties.ValueMember = "ID";

			gridLookUpEdit1.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
			gridLookUpEdit1.Properties.View.BestFitColumns();
			gridLookUpEdit1.Properties.ShowFooter = false;
			gridLookUpEdit1.Properties.View.OptionsView.ShowAutoFilterRow = true;
			gridLookUpEdit1.Properties.AutoComplete = false;
			gridLookUpEdit1.Properties.ImmediatePopup = true;
			gridLookUpEdit1.Properties.PopupFilterMode = PopupFilterMode.Contains;
			gridLookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
		}
	}
}

 


demo2效果如下:

 


demo2代码如下:

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;

   // 下拉框显示的字段数据.

   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;         

}