DevExpress如何实现对LookUpEdit的模糊查询

2025-11-01 04:51:34

1、我们利用一个TextEdit控件来作为搜索框,我们可以为该控件添加一些框内描述文字用来提示该控件是干什么用的,实现代码和效果图如下所示:

DevExpress.XtraEditors.TextEdit teSearch;

teSearch.Properties.NullValuePromptShowForEmptyValue = true;

teSearch.Properties.NullValuePrompt = "检索信息...";

DevExpress如何实现对LookUpEdit的模糊查询

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控件。

DevExpress如何实现对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();

        }

最终的实现效果如图所示。

DevExpress如何实现对LookUpEdit的模糊查询

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢