0

我查找了 .ValueMember 并发现它只能接受我假设的整数。我的问题是,如果我的联赛列返回 ABC,它可以同时是显示名称和组合框的值吗?如果有任何帮助来实现这一点,我们将不胜感激。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim con As New MySqlConnection("Server=xxx.xxx.xxx.xxx.;Database=database;Uid=username;Pwd=password;")

    Dim da As New MySqlDataAdapter("SELECT league FROM league_names ORDER BY league ASC", con)
    Dim dt As New DataTable

    da.Fill(dt)

    'Display table data in combobox
    ComboBox1.DisplayMember = "league"
    ComboBox1.ValueMember = "league"
    ComboBox1.DataSource = dt

End Sub
4

1 回答 1

0

这是对我有用的更新代码。我刚刚将所有 mysql 数据添加到 DataGridView 中,然后使用 for each 循环来获取名称。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim con As New MySqlConnection("Server=xxx.xxx.xxx.xxx.;Database=database;Uid=username;Pwd=password;")

    Dim da As New MySqlDataAdapter("SELECT league FROM league_names ORDER BY league ASC", con)
    Dim dt As New DataTable

    da.Fill(dt)

    DataGridView1.DataSource = dt
    DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

    For Each row As DataGridViewRow In DataGridView1.Rows
        If Not row.IsNewRow Then
            ComboBox1.Items.Add(row.Cells(0).Value.ToString)
        End If
    Next

End Sub
于 2015-02-27T10:22:00.393 回答