Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式
在Winform開發(fā)中中,我們?yōu)榱朔奖憧蛻暨x擇,往往使用系統(tǒng)的字典數(shù)據(jù)選擇,畢竟選擇總比輸入來(lái)的快捷、統(tǒng)一,一般我們都會(huì)簡(jiǎn)單封裝一下,以便方便對(duì)控件的字典值進(jìn)行展示處理,本篇隨筆介紹DevExpress控件的幾種常見(jiàn)的字典綁定展示方式,希望我們?cè)趯?shí)際WInform項(xiàng)目中使用到。
1、常規(guī)下拉列表的處理
常規(guī)的處理方式,可能會(huì)使用ComboBoxEdit 控件來(lái)承載下拉列表,下拉列表的值可以是固定的列表,也可以來(lái)自字典的方式,具體根據(jù)實(shí)際情況而定,大概的效果如下所示。

單擊下拉列表,會(huì)展示一些常規(guī)的字典項(xiàng)目,如下效果所示。

如果使用控件原始方式,我們綁定控件的下拉列表值的做法如下所示。
combo.Properties.BeginUpdate();//可以加快 combo.Properties.Items.Clear(); combo.Properties.Items.AddRange(itemList); combo.Properties.EndUpdate();//可以加快
不過(guò)我們一般傾向于高效率的界面處理,一般會(huì)編寫各類型的界面控件的擴(kuò)展函數(shù)用于快速處理。

不同類型的控件我們用一個(gè)獨(dú)立的擴(kuò)展文件來(lái)處理,這樣方便維護(hù)的同時(shí),也方便借鑒完善。
例如對(duì)于上面的控件,我們的綁定方法的擴(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">是否加入空值選項(xiàng)</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)值以及是否追加空白項(xiàng)目的處理。
當(dāng)然,我們?yōu)榱诉m應(yīng)各種數(shù)據(jù)源的綁定方式,我們重載了很多不同的函數(shù)處理,如下截圖所示。

當(dāng)然對(duì)于其他同類型的下列列表控件也是這樣處理即可。這樣界面上,我們就可以指定調(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ù)字典的類型,來(lái)進(jìn)一步做一個(gè)擴(kuò)展函數(shù),來(lái)簡(jiǎn)化綁定的處理。
/// <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);
}使用了這些簡(jiǎn)化的擴(kuò)展函數(shù),我們可以對(duì)系統(tǒng)的字典,根據(jù)字典類型來(lái)進(jìn)行綁定了。
private void InitDictItem()
{
this.txtManufacture.BindDictItems("供貨商");
this.txtSearchManufacture.BindDictItems("供貨商");
this.txtSearchDept.BindDictItems("部門");
}如上代碼所示,簡(jiǎn)化了很多處理,就一個(gè)函數(shù)就可以綁定了系統(tǒng)字典類型的下拉列表了。
2、帶序號(hào)的GridLookUpEdit下拉列表
有時(shí)候,我們?cè)谝恍┏R?jiàn)的系統(tǒng)里面,經(jīng)??吹揭恍в行蛱?hào)的下拉列表,其實(shí)里面就是一個(gè)GridControl的控件,我們只需要賦值對(duì)應(yīng)的列表數(shù)據(jù)源,以及設(shè)置顯示的列內(nèi)容,并重寫下序號(hào)的展示處理就可以實(shí)現(xiàn)了。
我們先來(lái)看看實(shí)際的效果。

上面的列表是一個(gè)GridControl的內(nèi)在控件,我們使用這個(gè)GridLookUpEdit下拉列表控件的時(shí)候,設(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è)定列表的序號(hào)的寬度和顯示文本
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ù)來(lái)簡(jiǎn)化實(shí)際的代碼,那么應(yīng)該如何封裝這個(gè)GridLookupEdit的下列控件呢,我們編寫的擴(kuò)展函數(shù)代碼如下所示。
/// <summary>
/// 綁定控件的數(shù)據(jù)源
/// </summary>
/// <param name="lookup">控件對(duì)象</param>
/// <param name="dataSource">數(shù)據(jù)源</param>
/// <param name="displayMember">顯示字段</param>
/// <param name="valueMember">值字段</param>
/// <param name="showRowIndicator">是否顯示序號(hào)</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;
//重寫序號(hào)顯示,默認(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;
}這樣處理后,界面上簡(jiǎn)化了不少代碼,如下使用代碼所示。
var list = DictItemUtil.GetDictByDictType("備件類別");
var columns = new List<LookUpColumnInfo>()
{
new LookUpColumnInfo("Value", "顯示名稱")
};
this.txtProjectList2.BindDictItems(list, "Text", "Value", true, columns.ToArray());通過(guò)上面的兩種方式,我們可以得到常見(jiàn)的兩種下拉列表的數(shù)據(jù)綁定展示方式。



而對(duì)于其他類似的下拉列表,如樹形列表,帶搜索的SearchLookupEdit等控件,我們也可以用類似的方式編寫自定義的擴(kuò)展函數(shù),這樣我們使用起來(lái)就非常方便的了。類似我們下面的做法方式,分門別類的對(duì)它們進(jìn)行一些簡(jiǎn)單的封裝處理。

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
淺析C#數(shù)據(jù)類型轉(zhuǎn)換的幾種形式
本篇文章是對(duì)C#中數(shù)據(jù)類型轉(zhuǎn)換的幾種形式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
C#實(shí)現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換
這篇文章介紹了C#實(shí)現(xiàn)Array,List,Dictionary互相轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
聊聊C# 中HashTable與Dictionary的區(qū)別說(shuō)明
這篇文章主要介紹了聊聊C# 中HashTable與Dictionary的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
C#在后臺(tái)運(yùn)行操作(BackgroundWorker用法)示例分享
BackgroundWorker類允許在單獨(dú)的專用線程上運(yùn)行操作。如果需要能進(jìn)行響應(yīng)的用戶界面,而且面臨與這類操作相關(guān)的長(zhǎng)時(shí)間延遲,則可以使用BackgroundWorker類方便地解決問(wèn)題,下面看示例2013-12-12
Visual C#類的定義及實(shí)現(xiàn)方法實(shí)例解析
這篇文章主要介紹了Visual C#類的定義及實(shí)現(xiàn)方法實(shí)例解析,對(duì)于新手來(lái)說(shuō)有不錯(cuò)的借鑒學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-07-07
C# 獲取硬件參數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C# 獲取硬件參數(shù)的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
C#之如何實(shí)現(xiàn)多個(gè)子窗體切換效果
這篇文章主要介紹了C#之如何實(shí)現(xiàn)多個(gè)子窗體切換的效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

