DevExpress LookupEdit按回车键自动确认选择数据

非常好用的代码:
C# 全选
/// <summary>
/// LookupEdit按回车键自动确认选择数据
/// </summary>
/// <param name="editor"></param>
public static void HandleEnterKeyAcceptValue(this LookUpEdit editor)
{
editor.KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Enter)
{
// 优先级1:根据显示文本获取键值
var value1 = editor.Properties.GetKeyValueByDisplayText(editor.Text);
if (value1 != null)
{
editor.ClosePopup(); //必须先关闭下拉窗体
editor.SetBindingValue(value1, true);
e.Handled = true;
return;
}
// 优先级2:录入的文本作为键值获取数据源obj
var obj = editor.Properties.GetDataSourceRowByKeyValue(editor.Text);
if (obj == null) obj = editor.Properties.GetDataSourceRowByKeyValue(editor.Text.ToLower()); //查找全小写
if (obj == null) obj = editor.Properties.GetDataSourceRowByKeyValue(editor.Text.ToUpper()); //查找全大写
if (obj != null)
{
editor.ClosePopup(); //必须先关闭下拉窗体
var v = ObjectHelper.GetPropertyValue(obj, editor.Properties.ValueMember);
editor.SetBindingValue(v, true);
e.Handled = true;
return;
}
}
};
}

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网





