简体   繁体   English

来自MySQL的VB.NET ComboBox SelectedIndex

[英]VB.NET ComboBox SelectedIndex from MySQL

so I have done with show data form my database MySQL to ComboBox.. I'm using this code: 所以我已经完成了从数据库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

In my code, I give some comment and bold to show you what I want to do. 在我的代码中,我给出一些注释和粗体以向您展示我想做什么。

So I have a writer code in product an writer code in writer (writer code named as writer), and I join the table to show writer name from writer code. 因此,我在产品中有一个编写者代码,在编写者中有一个编写者代码(命名为writer的编写者代码),然后我加入表格以显示来自编写者代码的作者名称。 But in ComboBox I want to show all of writer for editing but the default value is index of the writer that same like in product table. 但是在ComboBox中,我想显示所有作者进行编辑,但是默认值是作者的索引,就像在产品表中一样。

1 1个

在此处输入图片说明

2 2

在此处输入图片说明

If you see my first image that have a writer name Sutarjo Adisusilo but when I view it and want to use it as a default value in combobox that show A. Kardiyat Wiharyanto as indexvalue number 0.. 如果您看到我的第一个图像的作者名称为Sutarjo Adisusilo,但是当我查看它并希望将其用作显示A. Kardiyat Wiharyanto的默认值的组合框中的默认值,索引值为0。

I need help how to change the default value to be same like the picture number 1 我需要帮助,如何将默认值更改为与图片编号1相同

Thanks 谢谢

I've updated this answer to an easier method. 我已将此答案更新为一种更简单的方法。 Double check the column names though. 仔细检查列名。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM