我正在尝试从数据库中检索记录并将它们分配给comboBox。我只是这样做但是当我尝试从comboBox中选择任何人时,我总是得到comboBox中最后一条记录的值。请帮我。这是我的代码。
command = new SqlCommand("Select supplier_id, supplier_name from tbl_supplier", MySqlConnection);
m_dr = command.ExecuteReader();
while (m_dr.Read())
{
comboBoxSupplier.Items.Add(m_dr["supplier_name"].ToString());
comboBoxSupplier.ValueMember = (m_dr["supplier_id"].ToString());
comboBoxSupplier.DisplayMember = (m_dr["supplier_name"].ToString());
}
m_dr.Close();
答案 0 :(得分:0)
相关问题的答案:Fill Combobox from database
cmbTripName.ValueMember =“supplier_id”; cmbTripName.DisplayMember =“supplier_name”;
指向循环外的列。 (我试过这个,它对我有用)
答案 1 :(得分:0)
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
int count = 0;
foreach (datarow row in dt)
{
count += 1;
comboBoxSupplier.Items.Add(dt[count]["supplier_name"].ToString());
}