来自MySQL的VB.NET ComboBox SelectedIndex

时间:2016-09-19 06:18:22

标签: mysql vb.net combobox

所以我已经完成了将我的数据库MySQL显示数据到ComboBox ..我使用这段代码:

Private Sub Get_Product()
    Connection()

    Dim command As New MySqlCommand
    Dim reader As MySqlDataReader
    Dim query As String = "SELECT * FROM product, writer, publisher WHERE product.writer = writer.writer AND product.publisher = publisher.publisher AND code = " & throwCode & " ORDER BY code"

    Dim queryWriter As String = "SELECT DISTINCT writer, writer_name FROM writer ORDER BY writer_name"
    Dim dataAdapter As New MySqlDataAdapter(queryWriter, conn)
    Dim dataSet As New DataSet
    dataAdapter.Fill(dataSet, "writer")

    Try
        command = New MySqlCommand(query, conn)
        reader = command.ExecuteReader

        While reader.Read
            TextBoxISBN.Text = reader("isbn")
            TextBoxTitle.Text = reader("title")
            TextBoxPage.Text = reader("page")

            With (ComboBoxWriter)
                .Items.Add("Select")
                .DataSource = dataSet.Tables("writer")
                .DisplayMember = "writer_name"
                .ValueMember = "writer"
                .SelectedIndex = 0 **// Give atention to this code**
            End With

            TextBoxYear.Text = reader("year")
            TextBoxCategory.Text = reader("category")
            TextBoxCallNumber.Text = reader("call_number")
            TextBoxWeight.Text = reader("weight")
            TextBoxPurchasePrice.Text = reader("purchase_price")
            TextBoxSellingPrice.Text = reader("selling_price")
            TextBoxDiscount.Text = reader("discount")
            TextBoxDescription.Text = reader("description")
            TextBoxTag.Text = reader("tag")

            TextBoxPusatPenerbit.Text = reader("pusat_penerbit")
            TextBoxMrican.Text = reader("mrican")
            TextBoxPaingan.Text = reader("paingan")

            If (Not IsDBNull(reader("picture"))) Then
                Dim byteImage() As Byte = reader("picture")
                Dim tempImage As New System.IO.MemoryStream(byteImage)
                PictureBoxPicture.Image = Image.FromStream(tempImage)
            End If
        End While

        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

在我的代码中,我会给出一些评论并大胆地向您展示我想要做的事情。

所以我在产品中有一个编写器代码,编写器中的编写器代码(编写器编写的编写器代码),我加入表来显示编写器代码中的编写器名称。但是在ComboBox中我想显示所有编辑器进行编辑,但默认值是编写器的索引,就像产品表中一样。

1

enter image description here

2

enter image description here

如果你看到我的第一张图片有一个作家名字Sutarjo Adisusilo,但当我查看它并希望将其用作组合框中的默认值时,显示A. Kardiyat Wiharyanto为索引值0 ..

我需要帮助如何将默认值更改为与图片编号1

相同

由于

1 个答案:

答案 0 :(得分:0)

我已将此答案更新为更简单的方法。请仔细检查列名称。

ComboBoxWriter.SelectedIndex = ComboBoxWriter.FindStringExact(reader("writer_name"))