WinForm實(shí)現(xiàn)為ComboBox綁定數(shù)據(jù)源并提供下拉提示功能
更新時(shí)間:2014年08月06日 11:45:47 投稿:shichen2014
這篇文章主要介紹了WinForm實(shí)現(xiàn)為ComboBox綁定數(shù)據(jù)源并提供下拉提示功能,是非常實(shí)用的功能,需要的朋友可以參考下
本文實(shí)例展示了WinForm實(shí)現(xiàn)為ComboBox綁定數(shù)據(jù)源并提供下拉提示功能,這是一個(gè)非常有實(shí)用價(jià)值的功能,具體實(shí)現(xiàn)方法如下:
主要功能代碼如下:
/// <summary>
/// 為ComboBox綁定數(shù)據(jù)源并提供下拉提示
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="combox">ComboBox</param>
/// <param name="list">數(shù)據(jù)源</param>
/// <param name="displayMember">顯示字段</param>
/// <param name="valueMember">隱式字段</param>
/// <param name="displayText">下拉提示文字</param>
public static void Bind<T>(this ComboBox combox, IList<T> list, string displayMember, string valueMember, string displayText)
{
AddItem(list, displayMember, displayText);
combox.DataSource = list;
combox.DisplayMember = displayMember;
if (!string.IsNullOrEmpty(valueMember))
combox.ValueMember = valueMember;
}
private static void AddItem<T>(IList<T> list, string displayMember, string displayText)
{
Object _obj = Activator.CreateInstance<T>();
Type _type = _obj.GetType();
if (!string.IsNullOrEmpty(displayMember))
{
PropertyInfo _displayProperty = _type.GetProperty(displayMember);
_displayProperty.SetValue(_obj, displayText, null);
}
list.Insert(0, (T)_obj);
}
使用示例:
List<CommonEntity> Sources = new List<CommonEntity>();
private void WinComboBoxToolV2Test_Load(object sender, EventArgs e)
{
CreateBindSource(5);
comboBox1.Bind(Sources, "Name", "Age", "--請(qǐng)選擇--");
}
private void CreateBindSource(int count)
{
for (int i = 0; i < count; i++)
{
CommonEntity _entity = new CommonEntity();
_entity.Age = i;
_entity.Name = string.Format("Yan{0}", i);
Sources.Add(_entity);
}
}
代碼運(yùn)行效果如下:

您可能感興趣的文章:
- WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法
- winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實(shí)例
- C#(WinForm) ComboBox和ListBox添加項(xiàng)及設(shè)置默認(rèn)選擇項(xiàng)
- C# Winform使用擴(kuò)展方法實(shí)現(xiàn)自定義富文本框(RichTextBox)字體顏色
- C#中WinForm程序退出方法技巧總結(jié)
- c# winform取消右上角關(guān)閉按鈕的實(shí)現(xiàn)方法
- WinForm窗體間傳值的方法
- c# winform多線程的小例子
- .Net WInform開發(fā)筆記(三)談?wù)勛灾瓶丶?自定義控件)
- c#中多線程訪問winform控件的若干問題小結(jié)
- Winform ComboBox如何獨(dú)立繪制下拉選項(xiàng)的字體顏色
相關(guān)文章
詳解C#中使用對(duì)象或集合的初始值設(shè)定項(xiàng)初始化的操作
這篇文章主要介紹了詳解C#中使用對(duì)象或集合的初始值設(shè)定項(xiàng)初始化的操作,文中分別講了對(duì)對(duì)象和字典的初始化,需要的朋友可以參考下2016-01-01
C#利用System.Uri轉(zhuǎn)URL為絕對(duì)地址的方法
這篇文章主要介紹了C#利用System.Uri轉(zhuǎn)URL為絕對(duì)地址的方法,涉及C#操作URL的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02
C#查找對(duì)象在ArrayList中出現(xiàn)位置的方法
這篇文章主要介紹了C#查找對(duì)象在ArrayList中出現(xiàn)位置的方法,涉及C#中IndexOf方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
c#圖片縮放圖片剪切功能實(shí)現(xiàn)(等比縮放)
c#圖片縮放剪切功能實(shí)現(xiàn),代碼中包含了c#圖片處理的一些基礎(chǔ)知識(shí),與大家分享2013-12-12
C# datatable 不能通過已刪除的行訪問該行的信息處理方法
采用datatable.Rows[i].Delete()刪除行后再訪問該表時(shí)出現(xiàn)出現(xiàn)“不能通過已刪除的行訪問該行的信息”的錯(cuò)誤2012-11-11

