[英]c# Binding values to combobox when editing
I have a windows forms application that has several comboboxes populated with sql database table values. 我有一个Windows窗体应用程序,其中有几个用SQL数据库表值填充的组合框。
this is a example of the code for one: 这是其中一个代码的示例:
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";
My main form has several records like: model, brand, type and when i want to edit a specific recor, I select the record and click the edit button, that enters the values into the corresponding textboxes and comboxes, like this: 我的主窗体有几条记录,例如:型号,品牌,类型,当我要编辑特定记录时,我选择记录并单击“编辑”按钮,这会将值输入到相应的文本框和组合框中,如下所示:
private void btnEdit_Click(object sender, EventArgs e)
{
this.txtID.Text = lvMain.SelectedItems[0].SubItems[0].Text;
this.cbBrand.SelectedText = lvMain.SelectedItems[0].SubItems[1].Text;
this.cbModel.SelectedText = lvMain.SelectedItems[0].SubItems[2].Text;
this.txtName.Text = lvMain.SelectedItems[0].SubItems[3].Text;
this.cbType.SelectedText = lvMain.SelectedItems[0].SubItems[4].Text;
}
works fine, i get the text to the combobox and texboxes, but the values of the comboboxes aren't selected, only the text is. 工作正常,我将文本输入到组合框和texbox,但是未选择组合框的值,仅选择了文本。 if I chose
SelectedValue
or SelectedItem
I get nothing. 如果选择
SelectedValue
或SelectedItem
我什么也不会得到。 I have the corresponding text from the selected item but I have to choose all over again the values from the comboboxes :( 我从选定的项目中获得了相应的文本,但我必须再次从组合框中选择值:(
was i clear ? 我清楚了吗? :P
:P
I got it working like this: 我让它像这样工作:
int xcb;
xcb = this.cbBrand.FindString(lvMain.SelectedItems[0].SubItems[1].Text);
this.cbBrand.SelectedIndex = xcb;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.