原理:通过获取SearchLookUpEdit下拉打开控件的搜索框Text,或者点击搜索按钮给搜索框Text注册一个KeyDown事件,每次点击回车的时候,刷新数据源。
1、重写SearchLookUpEdit
public class SearchLookUpEditEx : SearchLookUpEdit
{
public void DataBind()
{
//注册下拉打开事件
this.Properties.Popup += Properties_Popup;
this.Properties.DataSource =所需要的数据源;
this.Properties.DisplayMember = "Name";//需要显示的FileName
this.Properties.KeyMember = "Id";
this.Properties.View.Columns.Clear();
DevExpress.XtraGrid.Columns.GridColumn colInfo = new DevExpress.XtraGrid.Columns.GridColumn();
colInfo.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
colInfo.Name = Name;
colInfo.Caption = "名称";//需要显示的Caption
colInfo.FieldName = Name;//需要显示的FileName
this.Properties.View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
colInfo});
}
TextEdit findTextEdit;
private void Properties_Popup(object sender, EventArgs e)
{
//刷新数据源
Properties.View.RefreshData();
//循环找到输入查询条件的编辑器,设置事件
var c = FindControl(PopupForm, x => x.Name == "teFind");
var b = FindControl(PopupForm, x => x.Name.Contains("btFind"));
//搜索框 定义输入事件
findTextEdit = c as TextEdit;
if (findTextEdit != null)
{
findTextEdit.KeyDown -= FindTextEdit_KeyDown;
findTextEdit.KeyDown += FindTextEdit_KeyDown;
}
//搜索按钮 定义点击事件
var findButton = b as SimpleButton;
if (findButton != null)
{
findButton.Click -= FindButton_Click;
findButton.Click += FindButton_Click;
}
}
//点击搜索按钮搜索具体的数据
private void FindButton_Click(object sender, EventArgs e)
{
Search();
}
//点击搜索按钮搜索具体数据
private void FindTextEdit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
}
}
/// <summary>
/// 通过名称搜索具体数据
/// </summary>
void Search()
{
string name = findTextEdit.Text;
if (name + "" != "")
{
this.Properties.BeginUpdate();
this.Properties.DataSource = datas;//重新赋值数据源
this.ShowPopup();
}
}
}
2、代码解析:
1.DataBind()方法:
可以在加载控件的时候调用,是为了注册SearchLookUpEdit下拉打开事件,和绑定数据源;
2.private void Properties_Popup(object sender, EventArgs e)方法
是为了获取SearchLookUpEdit的下拉打开Form,从而获取到搜索框Text控件,实现注册KeyDown事件;
3.Search()方法
通过搜索框Text重新查询数据,再重新绑定数据源