zoukankan      html  css  js  c++  java
  • ListBox和ComboBox绑定数据简单例子

    1. 将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容

    //自定义了Person类(有Name,Age,Heigth等属性)
    
    List<Person> persons=new List<Person>();
    persons.Add(new Person("WuMiao",18,175));
    persons.Add(new Person("YeXinYv",20,170));
    persons.Add(new Person("WuDong",18,175));
    
    //ListBox控件实现
    lb_PersonsList.DataSource=persons;        //指定数据源
    lb_PersonList.DisplayMember="Name";    //界面显示的是人的名字
    
    //ComboBox控件实现  (与ListBox的实现类似)
    cmb_PersonList.DataSource=persons;
    cmb_PersonList.DisplayMember="Name";

    2. ComboBox绑定数据源并提供下拉提示功能

    /// <summary>
    /// 为ComboBox绑定数据源并提供下拉提示
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    /// <param name="combox">ComboBox</param>
    /// <param name="list">数据源</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", "--请选择--");
    }
     
    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);
      }
    }
    具体的使用操作代码

    3. 双向绑定

      ListBox控件的datasourse属性能绑定多种数据格式,如List表,Table表。如果绑定List表当数据源发生改变时,ListBox控件显示并不会跟着改变。

      使用BindingList<T>类能实现数据源改变后ListBox的实时更新。只需要把数据源添加到BindingList对象中,并将ListBox的datasource绑定为BindingList 对象。当对BindingList的数据进行发生增、删、或者指向新对象时ListBox界面将跟着变动。需要注意的是对数据源属性的修改并不会引起界面的更新。

      DataTable也能实现该功能。实现这一功能的原理是一个叫做双向绑定的功能。

    4. ListBox数据绑定并显示的问题


    以前以为可以根ASP.NET中的用法差不多,即

    ListBox listBox;
    listBox.DataSource = ds;
    listBox.DataTextField = "要显示的字段名";
    listBox.DataValueField = "id";
    listBox.DataBind();

    然后利用listBox.SelectedItem即可访问被选中的项的值,当然在WinForm中除了DataSource的属性还有,其他都没有了,WinForm就换成如下方式:

    listBox.DataSource = ds.Tables[0];
    listBox.DisplayMember = "carsnumber";
    listBox.ValueMember = "id";

    这 样便可在ListBox正确显示出来,并且利用listBox.SelectedValue可以得到选定项的对应的id, 但是当我用listBox.SelectedItem打算得到相应的carsnumber值时,确显示System.Data.DataRowView, 利用listBox.Item[]访问得到的结果是一样的。最后在网上搜搜看能不能找到答案,又在CSDN上搜了一下以前的帖子,最后找到了答案,如果要 循环访问绑定了的Text值和Value 值,可用如下方式:

    for( int i = 0; i < listBox.Items.Count; i++ )
    {
    DataRowView drv = listBox.Items[i] as DataRowView;
    if( drv != null )
    {
    MessageBox.Show( "Text:" + drv[listBox.DisplayMember].ToString() );
    MessageBox.Show( "Value:" + drv[listBox.ValueMember].ToString() );
    }
    }

    参考文章

    1. winform中的ListBox和ComboBox绑定数据用法实例

    2. WinForm实现为ComboBox绑定数据源并提供下拉提示功能

    3. WinForm中ListBox数据绑定问题

  • 相关阅读:
    融云使用
    cocoaPods使用
    电脑硬件
    拖图UI和纯代码UI
    7.2内存管理-ARC
    7内存管理-MRC
    数据刷新
    5.1音频播放
    2.6核心动画
    Git常用操作
  • 原文地址:https://www.cnblogs.com/arxive/p/5705437.html
Copyright © 2011-2022 走看看