DevExpresss LookUpEdit详解

2024-08-24 08:38

本文主要是介绍DevExpresss LookUpEdit详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

DevExpresss LookUpEdit详解


一、属性的基本介绍:

绑定数据源:
lookUpEdit.Properties.ValueMember = 实际要用的字段; //相当于Editvalue
lookUpEdit.Properties.DisplayMember =要显示的字段; //相当于Text
lookUpEdit.Properties.DataSource = 数据源;

常用属性:

Popupwidth 下拉框宽度
Nulltxt 空时的值
DropDownRows 下拉框行数
AllowNullInput =True,可用Ctrl+Delete清空選擇內容

判断是否选择下拉框:
if(this.lookUpEdit.Editvalue==null ||this.lookUpEdit.Editvalue.tostring()=="nulltext")
{
//提示信息,说明未选择下拉框
}
清空nullText值: 
lookUpEdit.Properties.nulltext=null;

设置nullText值:
    lookUpEdit.Properties.nulltext=“请您选择”;
使用lookUpEdit1的值:
变量=this.lookUpEdit.Editvalue.Tostring()  //是LookUpEdit.Properties.ValueMember的值
变量=this.lookUpEdit.Text.Trim()      //是LookUpEdit.Properties.DisplayMember 的值

 特别值得注意的是,有时候我们要使用lookUpEdit来实现combox的一些效果,在实际的使用过程中在程序加载的时候会默认的选择第一项,它的设置是:

lookUpEdit.Itemindex=0; //选择第一项

lookUpEdit.Itemindex=-1; //无选项,此时显示的是nullText值 其实这个地方只要Editvalue==null,lookUpEdit就显示nullText

lookUpEdit1.Editvalue=value;//自动搜索datasouse,选择与之匹配的值,没有的情况下赋值null ,value的值必须与Valuemember的数据类型一致。

介绍三个重要的属性:
1. LookUpEdit.Properties.ImmediatePopup 在输入框按任一可见字符键时立即弹出下拉窗体。
2. LookUpEdit.Properties.AutoSearchColumnIndex 设置自动搜索的栏位序号,下拉窗体第一个栏位为0,依此类推,此属性配合SearchMode=OnlyInPopup时有效。
3. LookUpEdit.Properties.SearchMode 自动搜索定位模式
 

关于枚举类型SearchMode的定义:

C# Code:
// Summary:
// Enumerates search modes for a lookup edior.
public enum SearchMode
{
// Summary:
// The incremental search is enabled only when the dropdown window is open.
// If the window is closed, the user can modify the text in the edit box. However
// these changes are ignored.
// When the dropdown is open the incremental search is performed against the
// column whose index is specified by the DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit.AutoSearchColumnIndex
// property. The header of this column contains the search icon (binoculars).
// The user can click a specific column header to perform the search against
// this column.
// The following screenshot shows a sample lookup editor. The incremental search
// is performed against the second column.
OnlyInPopup = 0,
//
// Summary:
// Enables the automatic completion feature. In this mode, when the dropdown
// is closed, the text in the edit box is automatically completed if it matches
// a DevExpress.XtraEditors.Repository.RepositoryItemLookUpEditBase.DisplayMember
// field value of one of dropdown rows.
// When the dropdown is open, the automatic completion feature is disabled but
// the editor allows you to perform an incremental search in the same manner
// as when DevExpress.XtraEditors.Controls.SearchMode.OnlyInPopup mode is active.
AutoComplete = 1,
//
// Summary:
// Enables the incremental filtering feature. When you type within the edit
// box, the editor automatically opens the dropdown window and displays only
// records whose DevExpress.XtraEditors.Repository.RepositoryItemLookUpEditBase.DisplayMember
// field value starts with the characters typed. Other records are not displayed.
// If you enter a value that does not match any record, the dropdown window
// will not contain any rows.
// The following image shows a lookup editor when AutoFilter mode is enabled.
AutoFilter = 2,
}

OnlyInPopup : 配合ImmediatePopup=True时使用,当用户在输入框按任一可见字符键时立即弹出下拉窗体,并跟据输入的字符从头部开始匹配AutoSearchColumnIndex属性指定栏位字段的值,第一个栏位为0.

特点:在下拉窗体能显示匹配结果(蓝底白字),但在输入框内不显示。
效果图如下:
 


AutoComplete: 配合ImmediatePopup=True时使用,当用户在输入框按任一可见字符键时立即弹出下拉窗体,并在输入框自动完成您想要输入的数据,同时下拉窗体自动匹配最佳记录。AutoComplete模式仅匹配DisplayMember对应字段的值。
特点:能在输入框显示匹配的数据,并且下拉窗体显示匹配的记录。
效果图如下:

 

AutoFilter: 配合ImmediatePopup=True时使用,当用户在输入框按任一可见字符键时立即弹出下拉窗体,并在输入框自动完成您想要输入的数据,同时下拉窗体自动过滤掉不匹配的记录。
特点:能在输入框显示匹配的数据,并过滤过不想要的记录。

二、具体的使用:

看过了上面属性的介绍,一般的使用已经够了,但有的情况下,允许用户自由输入,即输入的值不一定是在绑定的数据源中,光用上面的属性就不行了,因为就算你输入的内容不在数据库中,控件也会帮你选中数据源中第一条数据,清空你输入的数据,恼火。。可以用下面的方法解决:

The LookUp editor allows a user to enter values which cannot be found in the lookup list. A programmer should handle this situation, otherwise a new value is lost. The LookUp editor provides aProcessNewValue event for this.

First of all, you should set the SearchMode property to OnlyInPopup andTextEditStyle to Standard to enable free text entry.

There are two common approaches for handling the ProcessNewValue event:
1. Immediately insert the new record in the lookup table and generate a new ID for it.
2. Display a dialog, where a user can set values for a new data row.

示例代码1
List<std_MetaInfo> source = DataHelper.MetaInfos;//数据源EditorHelper.BindLookUpEdit(lueStdNO, source, "StdNO", "StdNO");//lueStdNO.ProcessNewValue += lue_ProcessNewValue;        //实现自由输入功能        private void lue_ProcessNewValue(object sender, ProcessNewValueEventArgs e)        {            RepositoryItemLookUpEdit edit = ((LookUpEdit)sender).Properties;            if (e.DisplayValue == null || edit.NullText.Equals(e.DisplayValue) || string.Empty.Equals(e.DisplayValue))                return;//为空或者选择项不变,不执行后续操作            std_MetaInfo meta = new std_MetaInfo();            meta.StdNO = e.DisplayValue.ToString();            source.Add(meta);//在数据源中添加一条记录,如果数据源是DataTable,添加DataRow,其他形式数据源解决方法类似            e.Handled = true;        }public class EditorHelper{        public static void BindLookUpEdit(LookUpEdit lue, object source, string value, string displayName)        {            lue.Properties.DataSource = source;            lue.Properties.DisplayMember = displayName;            lue.Properties.ValueMember = value;            lue.Properties.NullText = "";            lue.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;//要使用户可以输入,这里须设为Standard            lue.Properties.SearchMode = SearchMode.AutoFilter;//自动过滤掉不需要显示的数据,可以根据需要变化        }}
示例代码2
        private void LookUpEdit1_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)        {            DataRow Row;            RepositoryItemLookUpEdit Edit = ((LookUpEdit)sender).Properties;            if (e.DisplayValue == null || Edit.NullText.Equals(e.DisplayValue) || string.Empty.Equals(e.DisplayValue))                return;            using (Form2 f = new Form2())            {                f.ItemID = "(Auto Number)";                f.ItemName = e.DisplayValue.ToString();//ItemName是Form2中的一个属性,return Form2中一个文本框的值                if (f.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)                {                    e.DisplayValue = f.ItemName;                    Row = LookupTable.NewRow();                    Row["Name"] = f.ItemName;                    LookupTable.Rows.Add(Row);                }            }            e.Handled = true;        }

这篇关于DevExpresss LookUpEdit详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1101989

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Java中八大包装类举例详解(通俗易懂)

《Java中八大包装类举例详解(通俗易懂)》:本文主要介绍Java中的包装类,包括它们的作用、特点、用途以及如何进行装箱和拆箱,包装类还提供了许多实用方法,如转换、获取基本类型值、比较和类型检测,... 目录一、包装类(Wrapper Class)1、简要介绍2、包装类特点3、包装类用途二、装箱和拆箱1、装