如何使用Listview填充表单

时间:2012-10-11 04:21:12

标签: vb.net

我已经成功地自学了如何从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

0 个答案:

没有答案