DevExpress如何实现对LookUpEdit的模糊查询
1、我们利用一个TextEdit控件来作为搜索框,我们可以为该控件添加一些框内描述文字用来提示该控件是干什么用的,实现代码和效果图如下所示:
DevExpress.XtraEditors.TextEdit teSearch;
teSearch.Properties.NullValuePromptShowForEmptyValue = true;
teSearch.Properties.NullValuePrompt = "检索信息...";

2、为LookUpEdit控件绑定一个实体列表数据,作为数据源,绑定的代码如下所示:
for (int i = 0; i < 3000; i++)
{
LookUpEditEntity entity = new LookUpEditEntity();
entity.ID = i.ToString();
entity.Name = "测试数据" + (i + 1).ToString();
_listEntity.Add(entity);
}
lueTest.Properties.DataSource = _listEntity;
lueTest.Properties.DropDownRows = _listEntity.Count;
lueTest.Properties.ValueMember = "ID";
lueTest.Properties.DisplayMember = "Name";
lueTest.Properties.ShowHeader = false;
lueTest.Properties.ShowFooter = false;
其中,lueTest为LookUpEdit控件。

3、接下来要在TextEdit的TextChanged事件中添加对LookUpEdit的模糊查询代码,当我们在TextEdit中输入文字时就会触发TextChanged事件,进而可以对LookUpEdit的数据源进行过滤查询。实现代码如下:
private void teSearch_TextChanged(object sender, EventArgs e)
{
string content = teTest.Text.Trim();
if (string.IsNullOrEmpty(content))
{
lueTest.ClosePopup();
lueTest.Properties.DataSource = _listEntity;
lueTest.Properties.DropDownRows = _listEntity.Count;
return;
}
List<LookUpEditEntity> newList = _listEntity.FindAll(t => t.Name.Contains(content));
lueTest.Properties.DataSource = newList;
lueTest.Properties.DropDownRows = newList.Count;
lueTest.ShowPopup();
}
最终的实现效果如图所示。
