mysql - 来自 MySQL 的 VB.NET ComboBox SelectedIndex

标签 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 相同

谢谢

最佳答案

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

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

关于mysql - 来自 MySQL 的 VB.NET ComboBox SelectedIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39566515/

相关文章:

php - date() 函数的问题

javascript - 在javascript src google map api中调用网络配置值

mysql - vb.net INSERT CURDATE() 到 MySQL 问题

c# - 可编辑的 ComboBox 绑定(bind)和更新源触发器

mysql - 使用 ID 匹配的另一个表中的数据更新表

php - 根据复选框值将是或否插入 MySQL

javascript - "Failed to load resource: the server responded with a status of 500 (Internal Server Error)"

wpf - 自动建议组合框 - MVVM

vba - 从组合框中删除所有项目?

Mysql 根据大于条件减去两列