Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式
在Winform開發(fā)中中,我們?yōu)榱朔奖憧蛻暨x擇,往往使用系統(tǒng)的字典數(shù)據(jù)選擇,畢竟選擇總比輸入來的快捷、統(tǒng)一,一般我們都會簡單封裝一下,以便方便對控件的字典值進(jìn)行展示處理,本篇隨筆介紹DevExpress控件的幾種常見的字典綁定展示方式,希望我們在實際WInform項目中使用到。
1、常規(guī)下拉列表的處理
常規(guī)的處理方式,可能會使用ComboBoxEdit 控件來承載下拉列表,下拉列表的值可以是固定的列表,也可以來自字典的方式,具體根據(jù)實際情況而定,大概的效果如下所示。
單擊下拉列表,會展示一些常規(guī)的字典項目,如下效果所示。
如果使用控件原始方式,我們綁定控件的下拉列表值的做法如下所示。
combo.Properties.BeginUpdate();//可以加快 combo.Properties.Items.Clear(); combo.Properties.Items.AddRange(itemList); combo.Properties.EndUpdate();//可以加快
不過我們一般傾向于高效率的界面處理,一般會編寫各類型的界面控件的擴(kuò)展函數(shù)用于快速處理。
不同類型的控件我們用一個獨(dú)立的擴(kuò)展文件來處理,這樣方便維護(hù)的同時,也方便借鑒完善。
例如對于上面的控件,我們的綁定方法的擴(kuò)展函數(shù)如下所示。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="combo">下拉列表控件</param> /// <param name="itemList">數(shù)據(jù)字典列表</param> /// <param name="defaultValue">控件默認(rèn)值</param> /// <param name="emptyFlag">是否加入空值選項</param> public static void BindDictItems(this ComboBoxEdit combo, List<CListItem> itemList, string defaultValue, bool emptyFlag = true) { combo.Properties.BeginUpdate();//可以加快 combo.Properties.Items.Clear(); combo.Properties.Items.AddRange(itemList); if (emptyFlag) { combo.Properties.Items.Insert(0, new CListItem("")); } if (itemList.Count > 0) { if (!string.IsNullOrEmpty(defaultValue)) { combo.SetComboBoxItem(defaultValue); } else { combo.SelectedIndex = 0; } } combo.Properties.EndUpdate();//可以加快 }
其中方法增加了一些默認(rèn)值以及是否追加空白項目的處理。
當(dāng)然,我們?yōu)榱诉m應(yīng)各種數(shù)據(jù)源的綁定方式,我們重載了很多不同的函數(shù)處理,如下截圖所示。
當(dāng)然對于其他同類型的下列列表控件也是這樣處理即可。這樣界面上,我們就可以指定調(diào)用綁定的處理操作了
private void InitDictItem() { this.txtManufacture.Items.Clear(); this.txtManufacture.Items.AddRange(DictItemUtil.GetDictByDictType("供貨商")); this.txtBigType.Items.Clear(); this.txtBigType.Items.AddRange(DictItemUtil.GetDictByDictType("備件屬類")); }
或者,我們可以根據(jù)字典的類型,來進(jìn)一步做一個擴(kuò)展函數(shù),來簡化綁定的處理。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="control">下拉列表控件</param> /// <param name="dictTypeName">數(shù)據(jù)字典類型名稱</param> /// <param name="defaultValue">控件默認(rèn)值</param> /// <param name="emptyFlag">是否添加空行</param> public static void BindDictItems(this ComboBoxEdit control, string dictTypeName, string defaultValue, bool emptyFlag = true) { Dictionary<string, string> dict = BLLFactory<DictData>.Instance.GetDictByDictType(dictTypeName); List<CListItem> itemList = new List<CListItem>(); foreach (string key in dict.Keys) { itemList.Add(new CListItem(key, dict[key])); } control.BindDictItems(itemList, defaultValue, emptyFlag); }
使用了這些簡化的擴(kuò)展函數(shù),我們可以對系統(tǒng)的字典,根據(jù)字典類型來進(jìn)行綁定了。
private void InitDictItem() { this.txtManufacture.BindDictItems("供貨商"); this.txtSearchManufacture.BindDictItems("供貨商"); this.txtSearchDept.BindDictItems("部門"); }
如上代碼所示,簡化了很多處理,就一個函數(shù)就可以綁定了系統(tǒng)字典類型的下拉列表了。
2、帶序號的GridLookUpEdit下拉列表
有時候,我們在一些常見的系統(tǒng)里面,經(jīng)??吹揭恍в行蛱柕南吕斜恚鋵嵗锩婢褪且粋€GridControl的控件,我們只需要賦值對應(yīng)的列表數(shù)據(jù)源,以及設(shè)置顯示的列內(nèi)容,并重寫下序號的展示處理就可以實現(xiàn)了。
我們先來看看實際的效果。
上面的列表是一個GridControl的內(nèi)在控件,我們使用這個GridLookUpEdit下拉列表控件的時候,設(shè)置好GridControl的數(shù)據(jù)源和顯示的列就基本上可以了。
//綁定數(shù)據(jù)源和顯示的內(nèi)容、隱藏值 this.txtProjectList.Properties.DataSource = list; this.txtProjectList.Properties.ValueMember = "Value"; this.txtProjectList.Properties.DisplayMember = "Text"; //設(shè)置Grid顯示的列信息 var columns = new List<LookUpColumnInfo>() { new LookUpColumnInfo("Value", "顯示名稱") }; for (int i = 0; i < columns.Count; i++) { this.txtProjectList.Properties.View.CreateColumn(columns[i].FieldName, columns[i].Caption, columns[i].Width, true, UnboundColumnType.Bound, DefaultBoolean.False, FixedStyle.None); } //其他屬性設(shè)置 this.txtProjectList.Properties.ImmediatePopup = true; this.txtProjectList.Properties.TextEditStyle = TextEditStyles.Standard; this.txtProjectList.Properties.PopupWidthMode = DevExpress.XtraEditors.PopupWidthMode.ContentWidth; //設(shè)定列表的序號的寬度和顯示文本 this.txtProjectList.Properties.View.IndicatorWidth = 40; this.txtProjectList.Properties.View.CustomDrawRowIndicator += (s, e) => { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } };
那么如果我們需要使用擴(kuò)展函數(shù)來簡化實際的代碼,那么應(yīng)該如何封裝這個GridLookupEdit的下列控件呢,我們編寫的擴(kuò)展函數(shù)代碼如下所示。
/// <summary> /// 綁定控件的數(shù)據(jù)源 /// </summary> /// <param name="lookup">控件對象</param> /// <param name="dataSource">數(shù)據(jù)源</param> /// <param name="displayMember">顯示字段</param> /// <param name="valueMember">值字段</param> /// <param name="showRowIndicator">是否顯示序號</param> /// <param name="lookUpColumnInfos">顯示的列</param> /// <returns></returns> public static object BindDictItems(this GridLookUpEdit lookup, object dataSource, string displayMember, string valueMember, bool showRowIndicator = true, params LookUpColumnInfo[] lookUpColumnInfos) { lookup.Properties.DataSource = dataSource; lookup.Properties.DisplayMember = displayMember; lookup.Properties.ValueMember = valueMember; lookup.Properties.View.Columns.Clear(); for (int i = 0; i < lookUpColumnInfos.Length; i++) { lookup.Properties.View.CreateColumn(lookUpColumnInfos[i].FieldName, lookUpColumnInfos[i].Caption, lookUpColumnInfos[i].Width, true, UnboundColumnType.Bound, DefaultBoolean.False, FixedStyle.None); } lookup.Properties.ImmediatePopup = true; lookup.Properties.TextEditStyle = TextEditStyles.Standard; if (showRowIndicator) { lookup.Properties.View.IndicatorWidth = 40; //重寫序號顯示,默認(rèn)不顯示數(shù)值 lookup.Properties.View.CustomDrawRowIndicator += (s, e) => { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } }; } return dataSource; }
這樣處理后,界面上簡化了不少代碼,如下使用代碼所示。
var list = DictItemUtil.GetDictByDictType("備件類別"); var columns = new List<LookUpColumnInfo>() { new LookUpColumnInfo("Value", "顯示名稱") }; this.txtProjectList2.BindDictItems(list, "Text", "Value", true, columns.ToArray());
通過上面的兩種方式,我們可以得到常見的兩種下拉列表的數(shù)據(jù)綁定展示方式。
而對于其他類似的下拉列表,如樹形列表,帶搜索的SearchLookupEdit等控件,我們也可以用類似的方式編寫自定義的擴(kuò)展函數(shù),這樣我們使用起來就非常方便的了。類似我們下面的做法方式,分門別類的對它們進(jìn)行一些簡單的封裝處理。
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
C#實現(xiàn)字母與ASCII碼互相轉(zhuǎn)換
ASCII是基于拉丁字母的編碼系統(tǒng),也是現(xiàn)今最通用的單字節(jié)編碼系統(tǒng),本文主要為大家詳細(xì)介紹了如何使用C#實現(xiàn)字母與ASCII碼互轉(zhuǎn),需要的可以參考下2024-01-01C#實現(xiàn)ProperTyGrid自定義屬性的方法
這篇文章主要介紹了C#實現(xiàn)ProperTyGrid自定義屬性的方法,主要通過接口ICustomTypeDescriptor實現(xiàn),需要的朋友可以參考下2014-09-09