ComboBox并从datagrid显示

时间:2014-08-17 06:26:45

标签: mysql combobox vb.net-2010

有人能帮助我吗?我有一个组合框,其中的项目来自我的数据库。如果我下拉组合框并选择一个项目,datagridview必须显示firstname,lastname等。我在studentno的数据类型是varchar,int也不起作用。数据将来自两个不同的表

第二是如何刷新组合框?因为我需要关闭然后在将要插入组合框的数据之前重新打开程序。

Private Sub ComboBox6_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox6.SelectedIndexChanged
    conn.Open()
    query = "select student.studentno,student.firstname,student.middlename,student.lastname,subject.subjcode,subject.subjdesc from student,subject where student.studentno = '" & ComboBox6.SelectedText & "' "
    cmd = New MySqlCommand(query, conn)
    da.SelectCommand = cmd
    ds = New DataSet
    da.Fill(ds, "ds_student,subject")
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "ds_student,subject"

    conn.Close()
End Sub

1 个答案:

答案 0 :(得分:0)

我和你有同样的问题然后我想出了这个

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Folha de Obra com Tabela Clientes.accdb;Persist Security Info=True")

    Dim query As String

    Dim cmd As New OleDb.OleDbCommand

    Dim da As New OleDb.OleDbDataAdapter

    Dim ds As New DataSet

    conn.Open()

    query = ("select * from Obras where IdCliente = " & ComboBox1.ValueMember)

    cmd = New OleDb.OleDbCommand(query, conn)

    da.SelectCommand = cmd

    da.Fill(ds, "NomeObra")

    ComboBox2.DataSource = ds.Tables(0)

    ComboBox2.DisplayMember = "NomeObra"

    ComboBox2.ValueMember = "IdCliente"

    conn.Close()

End Sub

我希望它可以帮到你