GridLookUpEdit 实现下拉选择

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using System.Reflection;
using DevExpress.Data.Filtering;
using DevExpress.Data.Linq;
using DevExpress.Data.Linq.Helpers;
using DevExpress.XtraGrid.Columns;

namespace MaterialsBackBill
{
public partial class TestXtraForm : DevExpress.XtraEditors.XtraForm
{
public TestXtraForm()
{
InitializeComponent();
LoadZyzd();
SetGridLookUpEditMoreColumnFilter(gridLookUpEdit1.Properties);


}

//加载主要诊断
void LoadZyzd()
{

DataTable dt = new DataTable();
dt.Columns.Add("用户名");
dt.Columns.Add("姓名");
dt.Rows.Add("一", "张三");
dt.Rows.Add("二", "李四");
dt.Rows.Add("三", "王五");
dt.Rows.Add("四", "李七");
this.gridLookUpEdit1.Properties.Popup -= new EventHandler(gridLookUpEdit1_Properties_Popup);
this.gridLookUpEdit1.EditValueChanging -= new DevExpress.XtraEditors.Controls.ChangingEventHandler(gridLookUpEdit1_EditValueChanging);
this.gridLookUpEdit1.TextChanged -= new EventHandler(gridLookUpEdit1_TextChanged);
this.gridLookUpEdit1.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
this.gridLookUpEdit1.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
this.gridLookUpEdit1.Properties.AutoComplete = false;
this.gridLookUpEdit1.Properties.ImmediatePopup = true;
this.gridLookUpEdit1.Properties.PopupFilterMode = DevExpress.XtraEditors.PopupFilterMode.Contains;
this.gridLookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
this.gridLookUpEdit1.Properties.DataSource = dt;
this.gridLookUpEdit1.Properties.ValueMember = "用户名";
this.gridLookUpEdit1.Properties.DisplayMember = "姓名";
this.gridLookUpEdit1.Properties.NullText = "";
this.gridLookUpEdit1.Properties.ImmediatePopup = true;
this.gridLookUpEdit1.Properties.ValidateOnEnterKey = true;
this.gridLookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
this.gridLookUpEdit1.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
this.gridLookUpEdit1.Properties.Popup += new EventHandler(gridLookUpEdit1_Properties_Popup);
this.gridLookUpEdit1.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(gridLookUpEdit1_EditValueChanging);
this.gridLookUpEdit1.TextChanged += new EventHandler(gridLookUpEdit1_TextChanged);
this.gridLookUpEdit1.Tag = dt;
}
//GridLookUpEdit模糊查询
private void FilterLookup(object sender, string[] arrFilterField)
{
//Text += " ! ";
GridLookUpEdit edit = sender as GridLookUpEdit;
GridView gridView = edit.Properties.View as GridView;
FieldInfo fi = gridView.GetType().GetField("extraFilter", BindingFlags.NonPublic | BindingFlags.Instance);
//Text = edit.AutoSearchText;
CriteriaOperator[] arrCriteriaOperator = new CriteriaOperator[arrFilterField.Length];
for (int i = 0; i < arrFilterField.Length; i++)
{
arrCriteriaOperator[i] = new BinaryOperator(arrFilterField[i], "%" + edit.AutoSearchText + "%", BinaryOperatorType.Like);
}
string filterCondition = new GroupOperator(GroupOperatorType.Or, arrCriteriaOperator).ToString();
fi.SetValue(gridView, filterCondition);
MethodInfo mi = gridView.GetType().GetMethod("ApplyColumnsFilterEx", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(gridView, null);
}

 

//绑定事件
void gridLookUpEdit1_Properties_Popup(object sender, EventArgs e)
{
// FilterLookup(sender, new string[] { "用户名", "姓名" });//中文,编码,拼音
}
void gridLookUpEdit1_TextChanged(object sender, EventArgs e)
{
// GridLookUpEdit edit = sender as GridLookUpEdit;
// edit.SelectionStart = edit.Text.Length;
}
void gridLookUpEdit1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{

//DataRow[] drs = ((DataTable)(gridLookUpEdit1.Tag)).Select("");
//this.BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate
//{
// FilterLookup(sender, new string[] { "用户名", "姓名" });
//}));
}

/// <summary>
/// 设置GridLookUpEdit多列过滤
/// </summary>
/// <param name="repGLUEdit">GridLookUpEdit的知识库,eg:gridlookUpEdit.Properties</param>
void SetGridLookUpEditMoreColumnFilter(DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit repGLUEdit)
{
repGLUEdit.EditValueChanging += (sender, e) =>
{
this.BeginInvoke(new System.Windows.Forms.MethodInvoker(() => {
GridLookUpEdit edit = sender as GridLookUpEdit;
DevExpress.XtraGrid.Views.Grid.GridView view = edit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
//获取GriView私有变量
System.Reflection.FieldInfo extraFilter = view.GetType().GetField("extraFilter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
List<DevExpress.Data.Filtering.CriteriaOperator> columnsOperators = new List<DevExpress.Data.Filtering.CriteriaOperator>();
string filterCondition = "";
foreach (GridColumn col in view.VisibleColumns)
{
if (col.Visible && col.ColumnType == typeof(string))
{
if (filterCondition.Length > 0)
{
filterCondition += string.Format(" OR {0} like '%{1}%' ", col.FieldName, edit.Text);
}
else
{
filterCondition += string.Format("{0} like '%{1}%' ", col.FieldName, edit.Text.Trim());
}
}
}

//filterCondition = string.Format("用户名 like '%{0}%' Or 姓名 like '%{0}%'", edit.Text);
extraFilter.SetValue(view, filterCondition);
//获取GriView中处理列过滤的私有方法
System.Reflection.MethodInfo ApplyColumnsFilterEx = view.GetType().GetMethod("ApplyColumnsFilterEx", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
ApplyColumnsFilterEx.Invoke(view, null);
}));
};
}

private void timer1_Tick(object sender, EventArgs e)
{
gridLookUpEdit1.ShowPopup();
timer1.Enabled = false;
}

private void gridLookUpEdit1_Enter(object sender, EventArgs e)
{
timer1.Enabled = true;
}

private void gridLookUpEdit1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
//private void initGridLookUpEdit()
//{
// DataTable dtt = new DataTable();
// dtt.Columns.Add("Stu_ID", typeof(string));
// dtt.Columns.Add("Stu_Number", typeof(string));
// dtt.Columns.Add("Stu_Name", typeof(string));

// for (int i = 0; i < 10; i++)
// {
// DataRow dr = dtt.NewRow();
// dr["Stu_ID"] = i.ToString();
// dr["Stu_Number"] = "学号:" + i.ToString();
// dr["Stu_Name"] = "姓名" + i.ToString();
// dtt.Rows.Add(dr);
// }

// DataView dv = new DataView(dtt);

// dtt = dv.ToTable(true, new string[] { "Stu_ID", "Stu_Number", "Stu_Name" });
// //绑定
// gridLookUpEdit1.Properties.DataSource = dtt;
// gridLookUpEdit1.Properties.ValueMember = "Stu_ID";
// gridLookUpEdit1.Properties.DisplayMember = "Stu_Name";

// this.gridLookUpEdit1.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
// //默认值
// this.gridLookUpEdit1.Properties.NullText = "";
// //文本居中对齐
// //this.gridLookUpEdit1.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
// this.gridLookUpEdit1.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
// this.gridLookUpEdit1.Properties.View.BestFitColumns();

// //显示不显示grid上第一个空行,也是用于检索的应用
// this.gridLookUpEdit1.Properties.View.OptionsView.ShowAutoFilterRow = false;
// //下面两行是为了更好的用户体验
// //this.gridLookUpEdit1.Properties.AutoComplete = false;
// //this.gridLookUpEdit1.Properties.ImmediatePopup = true;
// //this.gridLookUpEdit1.Properties.PopupFilterMode = DevExpress.XtraEditors.PopupFilterMode.Contains;
// //能否够输入内容进行检索(还是仅仅能使用选择输入)
// //this.gridLookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
// //是否显示底部
// this.gridLookUpEdit1.Properties.ShowFooter = false;

//}

}

}

上一篇:Devexpress 如何控制gridview 不能编辑修改


下一篇:构建企业级的仪表盘界面,这款工具的新功能值得一看