DevExpress控件库----LookUpEdit控件 和 GridLookUpEdit控件使用
DevExpress控件库----LookUpEdit控件 和 GridLookUpEdit控件使用
一.LookUpEdit控件简单使用: 下面是使用到的基础数据
public class Product { public int Id { get; set; } public string Model { get; set; } public string Name { get; set; } } public class Entility { private static string GetChar(int number) { string[] array = new string[] { "A","B","C","D","E","F","G","H","I"}; string result = array[number % 9]; return result; } private static string GetName(int number) { string[] array = { "Unitch数据采集器","MS扫描枪","105SL","TSC","PH880","MS320便携式打印机","PA700","DSX800电脑","HP打印机" }; string result = array[number % 9]; return result; } public static List<Product> GetProductList() { List<Product> list = new List<Product>(); for (int i = 0; i < 200; i++) { Product product = new Product() { Id=100+i,Model=GetChar(i)+i.ToString()+"DLJ",Name=GetName(i)+i.ToString() }; list.Add(product); } return list; } public static DataTable GetDataTable() { DataTable dt = new DataTable(); dt.Columns.Add("Id",typeof(System.Int32)); dt.Columns.Add("Model", typeof(System.String)); dt.Columns.Add("Name", typeof(System.String)); for (int i = 0; i < 200; i++) { DataRow dr = dt.NewRow(); dr["Id"] = 100 + i; dr["Model"] = GetChar(i) + i.ToString() + "DLJ"; dr["Name"] = GetName(i) + i.ToString(); dt.Rows.Add(dr); } return dt; } }
先看一下效果:
数据绑定:
List<Product> list = Entility.GetProductList();
lookUpEdit1.Properties.DataSource = list;
添加显示的列:找到属性列表---添加要绑定的列。如果没有添加列 根据属性名称 自动生成列
简单属性设置:
//双击显示下拉列表 lookUpEdit1.Properties.ShowDropDown = ShowDropDown.DoubleClick; lookUpEdit1.Properties.ImmediatePopup = true;//显示下拉列表 lookUpEdit1.Properties.TextEditStyle = TextEditStyles.Standard;//允许输入 lookUpEdit1.Properties.DropDownRows = 12;//默认显示的行 lookUpEdit1.Properties.PopupWidth = 350;//设置宽带 lookUpEdit1.Properties.NullText = "";//清空默认值
二.GridLookUpEdit控件简单使用
效果:
数据绑定:
List<Product> list = Entility.GetProductList();
gridLookUpEdit1.Properties.DataSource = list;
添加显示的数据列:并进行数据绑定 设置列的宽度
GridLoolUpEdit 默认 是根据 DisplayMember 绑定的字段 进行模糊筛选。
设置根据多列筛选功能:
private void FilterLookup(object sender) { GridLookUpEdit edit = sender as GridLookUpEdit; GridView gridView = edit.Properties.View as GridView; FieldInfo fi = gridView.GetType().GetField("extraFilter", BindingFlags.NonPublic | BindingFlags.Instance); BinaryOperator op1 = new BinaryOperator("Id", "%" + edit.AutoSearchText + "%", BinaryOperatorType.Like); BinaryOperator op2 = new BinaryOperator("Model", "%" + edit.AutoSearchText + "%", BinaryOperatorType.Like); BinaryOperator op3 = new BinaryOperator("Name", "%" + edit.AutoSearchText + "%", BinaryOperatorType.Like); string filterCondition = new GroupOperator(GroupOperatorType.Or, new CriteriaOperator[] { op1, op2,op3 }).ToString(); fi.SetValue(gridView, filterCondition); MethodInfo mi = gridView.GetType().GetMethod("ApplyColumnsFilterEx", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(gridView, null); } private void gridLookUpEdit1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e) { BeginInvoke(new MethodInvoker(delegate() { FilterLookup(sender); })); }
对筛选的列 显示行号
private void gridLookUpEdit1View_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } }
一些简单的属性设置:
//双击显示下拉列表 gridLookUpEdit1.Properties.ShowDropDown = ShowDropDown.DoubleClick; gridLookUpEdit1.Properties.ImmediatePopup = true;//显示下拉列表 gridLookUpEdit1.Properties.TextEditStyle = TextEditStyles.Standard;//允许输入 gridLookUpEdit1.Properties.NullText = "";//清空默认值
设置下拉列表宽度
如果有错误的地方,欢迎同行指正!!