最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)行效果如下:

相關(guān)文章

最新評(píng)論

宜黄县| 隆子县| 临武县| 家居| 安平县| 腾冲县| 濮阳市| 建昌县| 苗栗市| 宁国市| 屯昌县| 周至县| 寿光市| 泸州市| 隆子县| 城步| 南城县| 子洲县| 沙坪坝区| 承德县| 房产| 焉耆| 玉山县| 商河县| 逊克县| 张家港市| 牡丹江市| 抚松县| 黄大仙区| 乌兰县| 紫阳县| 溆浦县| 长沙县| 射阳县| 宁强县| 滨州市| 五原县| 禄丰县| 龙门县| 英吉沙县| 阳山县|