【DevExpresss】3、LookUpEdit详解

哈,今天又用到了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 a ProcessNewValue event for this.

First of all, you should set the SearchMode property to OnlyInPopup and TextEditStyle 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;
}

上面的够一般使用了,以后有什么更高级的用法再添加了,暂时就这样子了。。。

参考网址:
https://www.devexpress.com/Support/Center/p/A238.aspx

http://www.cnblogs.com/liran/archive/2009/02/28/1400402.html

http://blog.sina.com.cn/s/blog_6d1c583c01011qiv.html

【DevExpresss】3、LookUpEdit详解(转载)的更多相关文章

  1. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  2. jQuery的deferred对象详解(转载)

    本文转载自: jQuery的deferred对象详解(转载)

  3. Java 反射 设计模式 动态代理机制详解 [ 转载 ]

    Java 反射 设计模式 动态代理机制详解 [ 转载 ] @author 亦山 原文链接:http://blog.csdn.net/luanlouis/article/details/24589193 ...

  4. GridView内容详解(转载)

    GridView内容详解(转载) GridView是ASP.NET界面开发中的一个重要的控件,对GridView使用的熟练程度直接影响软件开发的进度及功能的实现.(车延禄)GridView的主要新特性 ...

  5. 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)

    如约而至,Java 10 正式发布!   3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...

  6. Linux下的I/O复用与epoll详解(转载)

    Linux下的I/O复用与epoll详解 转载自:https://www.cnblogs.com/lojunren/p/3856290.html  前言 I/O多路复用有很多种实现.在linux上,2 ...

  7. C#中的 特性 详解(转载)

    本篇幅转载于:http://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html C#中特性详解 特性提供了功能强大的方法,用于将元数据或声明信 ...

  8. Python安装、配置图文详解(转载)

    Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(I ...

  9. Oracle外部表详解(转载)

    (外部表创建主要注意创建目录访问权限问题.目录路径格式无空格等不相关字符,即必须是当前表访问用户可以访问:关于表中行数的限制问题,如果不加限制注意添加reject limit unlimited:表中 ...

随机推荐

  1. CLR查找和加载程序集的方式

    C#开发者在开发WinForm程序.Asp.Net Web(MVC)程序等,不可避免的在项目中引用许多第三方的DLL程序集, 编译后引用的dll都放在根目录下.以我个人作品 AutoProject S ...

  2. asp.net 后台任务作业框架收集

    收集几个可以用于 asp.net 的后台任务工具库并简单介绍. hangfire.io 支持 单次任务(Fire-and-forget),延时任务(Delayed),重复任务(Recurring ), ...

  3. 3403: [Usaco2009 Open]Cow Line 直线上的牛

    3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 62[S ...

  4. 【转】nginx配置:location配置方法及实例详解

    location匹配的是nginx的哪个变量? $request_uri location的匹配种类有哪些? 格式 location [ 空格 | = | ~ | ~* | !~ | !~* ] /u ...

  5. ubuntu nsight上链接OpenGL

    写一个需要使用OpenGL的程序,右击该程序名,此处需要OpenGL库的程序为Julia-C 右击,选择属性,弹出属性对话框,在左边选择build下的设置,中间窗格中选择GCC C++ Linker下 ...

  6. static成员是可以被其所在class创建的实例访问!!!

    <span style="font-family: Arial, Helvetica, sans-serif; ">关于静态方法以及静态变量的使用就不详细的说了,我就这 ...

  7. 微信小程序-开发入门

    微信小程序已经火了一段时间了,之前一直也在关注,就这半年的发展来看,相对原生APP大部分公司还是不愿意将主营业务放到微信平台上,以免受制于腾讯,不过就小程序的应用场景(用完即走和二维码分发等)还是很值 ...

  8. less可以做什么?less详解!(less嵌套选择器实现纯CSS二级导航)

    前端技术众多,作为一名前端工程师,我们每接触新技术首先要懂得此技术的优势和劣势,这是最基本的.往深入了说还需要懂得技术的应用场景,配合的技术等,方便为以后架构做准备.而less作为一门CSS预处理语言 ...

  9. iOS开发之数据存储之NSData

    1.概述 使用archiveRootObject:toFile:方法可以将一个对象直接写入到一个文件中,但有时候可能想将多个对象写入到同一个文件中,那么就要使用NSData来进行归档对象. NSDat ...

  10. VisualVM监控远程主机上的JAVA应用程序

    使用VisualVM监控远程主机上JAVA应用程序时,需要开启远程主机上的远程监控访问,或者在远程JAVA应用程序启动时,开启远程监控选项,两种方法,选择其中一种就可以开启远程监控功能,配置完成后就可 ...