Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式_第1頁
Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式_第2頁
Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式_第3頁
Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式_第4頁
Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式在Winform開發(fā)中中,我們?yōu)榱朔奖憧蛻暨x擇,往往使用系統(tǒng)的字典數(shù)據(jù)選擇,畢竟選擇總比輸入來的快捷、統(tǒng)一,一般我們都會(huì)簡(jiǎn)單封裝一下,以便方便對(duì)控件的字典值進(jìn)行展示處理,本篇隨筆介紹DevExpress控件的幾種常見的字典綁定展示方式,希望我們?cè)趯?shí)際WInform項(xiàng)目中使用到。

1、常規(guī)下拉列表的處理

常規(guī)的處理方式,可能會(huì)使用ComboBoxEdit控件來承載下拉列表,下拉列表的值可以是固定的列表,也可以來自字典的方式,具體根據(jù)實(shí)際情況而定,大概的效果如下所示。

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

如果使用控件原始方式,我們綁定控件的下拉列表值的做法如下所示。

combo.Properties.BeginUpdate();//可以加快

combo.Properties.Items.Clear();

combo.Properties.Items.AddRange(itemList);

combo.Properties.EndUpdate();//可以加快

不過我們一般傾向于高效率的界面處理,一般會(huì)編寫各類型的界面控件的擴(kuò)展函數(shù)用于快速處理。

不同類型的控件我們用一個(gè)獨(dú)立的擴(kuò)展文件來處理,這樣方便維護(hù)的同時(shí),也方便借鑒完善。

例如對(duì)于上面的控件,我們的綁定方法的擴(kuò)展函數(shù)如下所示。

///summary

///綁定下拉列表控件為指定的數(shù)據(jù)字典列表

////summary

///paramname="combo"下拉列表控件/param

///paramname="itemList"數(shù)據(jù)字典列表/param

///paramname="defaultValue"控件默認(rèn)值/param

///paramname="emptyFlag"是否加入空值選項(xiàng)/param

publicstaticvoidBindDictItems(thisComboBoxEditcombo,ListCListItemitemList,stringdefaultValue,boolemptyFlag=true)

combo.Properties.BeginUpdate();//可以加快

combo.Properties.Items.Clear();

combo.Properties.Items.AddRange(itemList);

if(emptyFlag)

combo.Properties.Items.Insert(0,newCListItem(""));

if(itemList.Count0)

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)用綁定的處理操作了

privatevoidInitDictItem()

this.txtManufacture.Items.Clear();

this.txtManufacture.Items.AddRange(DictItemUtil.GetDictByDictType("供貨商"));

this.txtBigType.Items.Clear();

this.txtBigType.Items.AddRange(DictItemUtil.GetDictByDictType("備件屬類"));

}

或者,我們可以根據(jù)字典的類型,來進(jìn)一步做一個(gè)擴(kuò)展函數(shù),來簡(jiǎn)化綁定的處理。

///summary

///綁定下拉列表控件為指定的數(shù)據(jù)字典列表

////summary

///paramname="control"下拉列表控件/param

///paramname="dictTypeName"數(shù)據(jù)字典類型名稱/param

///paramname="defaultValue"控件默認(rèn)值/param

///paramname="emptyFlag"是否添加空行/param

publicstaticvoidBindDictItems(thisComboBoxEditcontrol,stringdictTypeName,stringdefaultValue,boolemptyFlag=true)

Dictionarystring,stringdict=BLLFactoryDictData.Instance.GetDictByDictType(dictTypeName);

ListCListItemitemList=newListCListItem

foreach(stringkeyindict.Keys)

itemList.Add(newCListItem(key,dict[key]));

control.BindDictItems(itemList,defaultValue,emptyFlag);

}

使用了這些簡(jiǎn)化的擴(kuò)展函數(shù),我們可以對(duì)系統(tǒng)的字典,根據(jù)字典類型來進(jìn)行綁定了。

privatevoidInitDictItem()

this.txtManufacture.BindDictItems("供貨商");

this.txtSearchManufacture.BindDictItems("供貨商");

this.txtSearchDept.BindDictItems("部門");

}

如上代碼所示,簡(jiǎn)化了很多處理,就一個(gè)函數(shù)就可以綁定了系統(tǒng)字典類型的下拉列表了。

2、帶序號(hào)的GridLookUpEdit下拉列表

有時(shí)候,我們?cè)谝恍┏R姷南到y(tǒng)里面,經(jīng)常看到一些帶有序號(hào)的下拉列表,其實(shí)里面就是一個(gè)GridControl的控件,我們只需要賦值對(duì)應(yīng)的列表數(shù)據(jù)源,以及設(shè)置顯示的列內(nèi)容,并重寫下序號(hào)的展示處理就可以實(shí)現(xiàn)了。

我們先來看看實(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顯示的列信息

varcolumns=newListLookUpColumnInfo()

newLookUpColumnInfo("Value","顯示名稱")

for(inti=0;icolumns.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.IsRowIndicatore.RowHandle=0)

e.Info.DisplayText=(e.RowHandle+1).ToString();

};

那么如果我們需要使用擴(kuò)展函數(shù)來簡(jiǎn)化實(shí)際的代碼,那么應(yīng)該如何封裝這個(gè)GridLookupEdit的下列控件呢,我們編寫的擴(kuò)展函數(shù)代碼如下所示。

///summary

///綁定控件的數(shù)據(jù)源

////summary

///paramname="lookup"控件對(duì)象/param

///paramname="dataSource"數(shù)據(jù)源/param

///paramname="displayMember"顯示字段/param

///paramname="valueMember"值字段/param

///paramname="showRowIndicator"是否顯示序號(hào)/param

///paramname="lookUpColumnInfos"顯示的列/param

///returns/returns

publicstaticobjectBindDictItems(thisGridLookUpEditlookup,objectdataSource,stringdisplayMember,stringvalueMember,boolshowRowIndicator=true,paramsLookUpColumnInfo[]lookUpColumnInfos)

lookup.Properties.DataSource=dataSource;

lookup.Properties.DisplayMember=displayMember;

lookup.Properties.ValueMember=valueMember;

lookup.Properties.View.Columns.Clear();

for(inti=0;ilookUpColumnInfos.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.IsRowIndicatore.RowHandle=0)

e.Info.DisplayText=(e.RowHandle+1).ToString();

returndataSource;

}

這樣處理后,界面上簡(jiǎn)化了不少代碼,如下使用代碼所示。

varlist=DictItemUtil.GetDictByDictType("備件類別");

varcolumns=newListLookUpColumnInfo()

newLookUpColumnInfo("Value","顯示名稱")

this.txtProjectList2.BindDictItems(list,"Tex

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論