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;        }