1

非常感谢您提供一点提示或帮助,谢谢。

DataBound ComboBox我的表中有一个MySql名为"formmaintenance",它有 3 列,即FormCode, FormName, FormDescription, Combobox DisplayMemberisFormCodeValueMemberis FormCode,我有 2 个TextBoxes 名为txtNameand txtDesc

我想要发生的是,当我在 Combobox 中选择一个项目(例如:form101)时,我想在on和on 上显示FormName所选项目(例如名称:Paper)的值。txtNameFormDescriptiontxtdesc

我还不知道如何开始,因为我对 Vb.Net 很陌生。

这是我的代码(更新

 If conn.State = ConnectionState.Closed Then
            conn.ConnectionString = "Server = localhost; Username= root; Password =; Database =forms"
            Try
                conn.Open()
                MsgBox("Connection Established")
                Dim dt As New DataTable

                Dim sql = "SELECT FormName FROM formma WHERE  FormCode = '" & cmbCode.Text & "';"
                Dim Result As String


                cmd.Connection = conn
                cmd.CommandText = sql
                Result = cmd.ExecuteScalar()
                da.SelectCommand = cmd

                dr = cmd.ExecuteReader()

                If Result IsNot Nothing Then
                    txtName.Text = Result.ToString()

                End If

            Catch ex As MySqlException
                Console.WriteLine("Error: " & ex.ToString())

            End Try
        End If

    End Sub
4

2 回答 2

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
于 2012-10-29T06:37:53.620 回答
2

如果我正确理解您的问题,这里是提示。

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

     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。

于 2012-10-29T07:13:18.877 回答