我有这个组合框从 SQL 中获取值:
public void brandSelectCB(ComboBox cb)
{
string sSQL = " SELECT" +
" id, name" +
" FROM" +
" tbBrand" +
" ORDER BY" +
" name";
sqlConnect connect = new sqlConnect();
DataTable dt = new DataTable();
dt = connect.getBD(sSQL);
cb.DataSource = dt;
cb.DisplayMember = "name";
cb.ValueMember = "id";
}
我也有一个这样的列表视图:
public void modelSelect(ListView lvModel)
{
string sSQL = " SELECT" +
" tbModel.id, tbBrand.name AS brand, tbModel.name" +
" FROM" +
" tbBrand, tbModel" +
" WHERE" +
" tbBrand.id = tbModel.brand" +
" ORDER BY" +
" tbBrand.name, tbModel.name";
sqlConnect connect = new sqlConnect();
DataTable dt = new DataTable();
dt = connect.getBD(sSQL);
foreach (DataRow dr in dt.Rows)
{
ListViewItem item = new ListViewItem(dr["id"].ToString());
item.SubItems.Add(dr["brand"].ToString());
item.SubItems.Add(dr["name"].ToString());
lvModel.Items.Add(item);
}
}
当我从列表视图中选择一行并单击编辑按钮时,它应该从该选定行获取值到相应的组合框和文本框:
private void btnEdit_Click(object sender, EventArgs e)
{
this.txtID.Text = lvModels.SelectedItems[0].SubItems[0].Text;
this.cbBrand.SelectedText = lvModels.SelectedItems[0].SubItems[1].Text;
this.txtName.Text = lvModels.SelectedItems[0].SubItems[2].Text;
}
但我在组合框上没有值,只有在文本框中。我意识到这是因为组合是下拉列表样式,我应该使用SelectedValue
或SelectedItem
但它们似乎都不起作用。
有任何想法吗 ?