我希望将数据添加到comboboxlist中,但不确定执行此操作的正确方法。数据来自原始SQL语句。
我直接从数据库中查看了绑定数据,但不清楚所有这些绑定和数据集对我来说是如何工作的,所以我决定跳过这一步,自己将数据插入到组合框中(在您的帮助下)。
我在网上看到的代码如下:
public partial class Form1 : Form {
// Content item for the combo box
private class Item {
public string Name;
public int Value;
public Item(string name, int value) {
Name = name; Value = value;
}
public override string ToString() {
// Generates the text shown in the combo box
return Name;
}
}
public Form1() {
InitializeComponent();
// Put some stuff in the combo box
comboBox1.Items.Add(new Item("Blue", 1));
comboBox1.Items.Add(new Item("Red", 2));
comboBox1.Items.Add(new Item("Nobugz", 666));
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
// Display the Value property
Item itm = (Item)comboBox1.SelectedItem;
Console.WriteLine("{0}, {1}", itm.Name, itm.Value);
}
}
Do you really have to create a new class to just to add data to a combobox ? Also, using the above technique my code looks like:
while (rdata.Read()){
String Name = (String)rdata["vetName"];
Name = Name.Trim();
String Surname = (String)rdata["vetSurname"];
Surname = Surname.Trim();
String id = rdata["vetID"].ToString().Trim();
MessageBox.Show("ID " + id);
int value1 = Convert.ToInt32(id);
MessageBox.Show("value1 " + value1);
String display = (String)Name + " " + Surname;
editVetComboBox.Items.Add(new Item(display, 2));
}
问题是,虽然组合框中填充了名字和姓氏<strong>,但没有添加值(ID)
有什么想法吗?
Many Thanks, Richard