ComboBox是Databound,我想在文本框中显示数据

时间:2012-10-29 03:25:17

标签: mysql vb.net winforms visual-studio-2010

非常感谢您的小小提示或帮助,谢谢。

我的DataBound ComboBoxMySql中有一个"formmaintenance",其中有3列FormCodeFormNameFormDescription,{{ 1}}是Combobox DisplayMemberFormCode也是ValueMember,我有2 FormCode被命名为TextBoxestxtName

我想要发生的是当我在Combobox中选择一个项目时(例如:form101)我想在txtDesc上显示所选项目的FormName值(例如:名称:纸张) txtName上的FormDescription

我还不知道如何从这开始,因为我是Vb.Net的新手。

这是我的代码(已更新

txtdesc

2 个答案:

答案 0 :(得分:2)

在组合框的SelectedIndex_Changed事件中,你应该写下面的

Dim st As String = "SERVER=localhost;DATABASE=forms;UID=root;"
Dim conn As New MySqlConnection(st)
Dim cmd As MySqlCommand = conn.CreateCommand()
cmd.CommandText = "SELECT FormName, FormDescription FROM formmaintenance WHERE  FormCode = '" + cmbCode.Text + "'"
conn.Open()
Dim dr As MySqlDataReader = cmd.ExecuteReader()
If dr.Read() Then
    txtName.Text = dr(0).ToString()
    txtdesc.Text = dr(1).ToString()
End If

答案 1 :(得分:2)

如果我理解你的问题,请点击这里。

1-从DB加载数据并将其绑定到组合框:

     Dim tableFormula as new DataTable("formula");
     using (Dim sqlConn as new SqlConnection("Server = localhost; Username= root; Password =; Database =forms"))
     {
        Dim sqlQuery  as string = "SELECT FormCode, FormName, FormDescription FROM formma;"
        Dim cmd as new SqlCommand(sqlQuery, sqlConn);
        Dim da  as new SqlDataAdapter(cmd); 
        //create dataTable and bind it with dataSet:
        da.Fill(tableFormula);
     }              
     //setting dataSource to comboBox:
     this.cmbCode.DataSource = new BindingSource(tableFormula, null);

2-在cmbCode组合框的SelectedIndex_Changed事件处理程序中,根据组合框的选定项设置文本。

        if(cmbCode.SelectedItem != null) 
        {
            txtName.Text = ((DataRow)cmbCode.SelectedItem)["FormName"].ToString();
            txtdesc.Text = ((DataRow)cmbCode.SelectedItem)["FormDescription"].ToString();
        }

HTH。