0

我已经成功地自学了如何从 MySQL 数据库中提取一些数据并将其绑定到 ListView 控件 (ListViewCard)。

现在我不知道如何使用 SelectedIndexChanged 事件遍历记录并填充表单上的其他一些控件(即 7 个​​文本框、2 个组合框和 2 个日期时间选择器)。

您的帮助将不胜感激。这是我的代码:

Private Sub loadCard()

    Try
        'FOR MySQL DATABASE USE
        'Dim dbQuery As String = ""
        'Dim dbCmd As New MySqlCommand
        'Dim dbAdapter As New MySqlDataAdapter
        Dim dbTable As New DataTable
        Dim i As Integer

        If dbConn.State = ConnectionState.Closed Then
            dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
            dbConn.Open()
        End If

        dbQuery = "SELECT *" & _
                   "FROM cc_master INNER JOIN customer ON customer.accountNumber = cc_master.customer_accountNumber " & _
                   "WHERE customer.accountNumber = '" & TextBoxAccount.Text & "'"
        With dbCmd
            .CommandText = dbQuery
            .Connection = dbConn
        End With
        With dbAdapter
            .SelectCommand = dbCmd
            .Fill(dbTable)
        End With
        ListViewCard.Items.Clear()
        For i = 0 To dbTable.Rows.Count - 1
            With ListViewCard
                .Items.Add(dbTable.Rows(i)("ccID"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(dbTable.Rows(i)("ccNumber"))
                    .Add(dbTable.Rows(i)("ccExpireMonth"))
                    .Add(dbTable.Rows(i)("ccExpireYear"))
                End With
            End With
        Next
    Catch ex As MySqlException
        MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
                    vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
    End Try
    dbConn.Close()

End Sub
4

0 回答 0