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數據綁定問題

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM