如图,因工作需要,所以需要动态绑定ComboBox,代码已经写好。也可以实现对ComboBox控件的值绑定。但是奇怪的是运行后显示的页面异常,如图所示。不知道是怎么回事。请大牛给看看,给出改进建议,谢谢。
附代码如下:
ComboBox comboBoxCarPlateColor = new ComboBox();
var listCarPlateColor = new List<KeyValuePair<string, string>>();
for (int i = 0; i < familynames.Length; i++)
{
listCarPlateColor.Add(new KeyValuePair<string, string>(Convert.ToString(i+1), familynames[i]));
}
this.com_family.Items.Clear();//清空ComboBox
comboBoxCarPlateColor.DataSource = listCarPlateColor;
comboBoxCarPlateColor.ValueMember = "key";
comboBoxCarPlateColor.DisplayMember = "value";
comboBoxCarPlateColor.Parent = this;//加入这一句
//comboBoxCarPlateColor.SelectedValue = "2";
//this.Controls.Add(comboBoxCarPlateColor); //搜索的方法是这样的写的,实际运行会报错。所以用了下面的代码
this.com_family.Controls.Add(comboBoxCarPlateColor);//改进后的代码