日期:2014-05-19  浏览次数:20887 次

请问如何使用FOR语句实现ComboBox类似于DataSource的功能
DataSource可以指定DisplayMember,和ValueMember
而FOR语句如何写呢?
ComboBox.Items.Add方法要求传入的是一个object   类型的对象,我该传什么

------解决方案--------------------
上面是最简单,也最常用的方法,如果确实有必要直接Add,可以这样做

using System.Collections;

for(int i=0;i <5;i++)
{
comboBox1.Items.Add(new DictionaryEntry( "test "+i, i));
}
comboBox1.ValueMember = "Key ";
comboBox1.DisplayMember = "Value ";

这样做以后,想得到选中项内容的时候,需要这样做
string value = ((DictionaryEntry)comboBox1.SelectedItem).Key.ToString();
string text = ((DictionaryEntry)comboBox1.SelectedItem).Value.ToString();