如果你的Ilist是List<IDictionary>的,就不行了,ComboBox的DisplayMember, ValueMember需要从对象的property里获取值,而IDictionary的只是Key,所以不行。如果你想绑定IList 或 ArrayList的话,就得用一个实体类(Java或ORM里的概念)来作为数据的存放对象,然后把对象Add到列表里,然后就可以给DataSource赋值了。
定义一个类,有两个属性
public class ValueObject{
private string _name;
private string _value;
internal Name
{
get { return _value; }
set { _value = value; }
}
}
然后,用一个
ArrayList list = new ArrayList();
for ( int i = 0 ; i < 3 ; i ++ )
{
ValueObject vo = new ValueObject();
vo.Name = " a " ;
vo.Value = " 1 " ;
list.add(vo);
}
ComboBox cbb = new ComboBox();
cbb.DataSource = list;
cbb.DisplayMember = " Name " ;
cbb.ValueMember = " Value " ;
这样就OK了,取值只要cbb.SelectedValue就取到了。
如果有做过java的话,这VO的作用就会很清楚的了。
public class ValueObject{
private string _name;
private string _value;
internal Name
{
get { return _value; }
set { _value = value; }
}
}
然后,用一个
ArrayList list = new ArrayList();
for ( int i = 0 ; i < 3 ; i ++ )
{
ValueObject vo = new ValueObject();
vo.Name = " a " ;
vo.Value = " 1 " ;
list.add(vo);
}
ComboBox cbb = new ComboBox();
cbb.DataSource = list;
cbb.DisplayMember = " Name " ;
cbb.ValueMember = " Value " ;
这样就OK了,取值只要cbb.SelectedValue就取到了。
如果有做过java的话,这VO的作用就会很清楚的了。
另外,很多人不知道怎样根据读取的value来选定列表,其实用
ComboBox.SelectedValue = "xxx"就可以选定的了。
下面有个蠢方法,是我发现这个方法前使用的
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)