0

早上好,

我整晚都在试图自己解决这个问题而不打扰其他人,但我做不到。

我已经成功地查询了我的 MySQL 数据库并将一组记录放入 DataTable (dbTable) 中。在调试过程中,我可以看到它的内容,所以我知道数据在那里。最初,DataTable 用于填充表单上的 ListView 控件。

当我选择一条记录时,我希望将 DataTable(或我刚刚运行的查询)的内容分配给一些 TextBox 控件。我似乎无法弄清楚如何做到这一点。任何帮助将不胜感激。

添加图像的更新: 我希望这些屏幕截图能让我了解我想要做什么。第一张图片显示了输入帐号后发生的情况。第二个框显示了在 ListView 中选择了一条记录后展开的 Groupbox 以显示表单字段。

控件名称为: TextBoxCustomer、TextBoxLastName、TextBoxFirstName、ComboBoxSalutation、ComboBoxCardType、TextBoxCard.Text、TextBoxExpireMonth、TextBoxExpireYear、TextBoxCVV2。

DataTable(dbTable)中的字段名有: nameCOMPANY、nameLAST、nameFIRST、nameSALUTATION、ccType、ccNumber、ccExpireMonth、ccExpireYear、ccCode。

图 1: 在 TextBoxAccount 中输入后。

图 2: 在列表视图中选择一条记录后

4

2 回答 2

2

你试过这个吗?

TextBox1.Text = dbTable.Rows(0)("ColumnName").ToString()
TextBox2.Text = dbTable.Rows(1)("OtherColumnName").ToString()

你也可以这样做:

Dim row as DataRow = dbTable.Rows(0)
TextBox1.Text = row("ColumnName").ToString()

row = dbTable.Rows(1)
TextBox2.Text = row("OtherColumnName").ToString()

您也可以使用asDataBind到一个DataGrid(或类似的控件),然后将其设置为。这将为您创建文本框控件。dbTableDataSourceDataGrid.EditModeTrue

更新:

尝试像这样将您的文本框绑定到您的 ListView 的选定值:

Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    Dim item As ListViewItem = Nothing
    Dim tb As TextBox = Nothing
    Dim i As Integer = 0

    For Each item In ListView1.SelectedItems
        tb = Me.Controls.Find("TextBox" & i.ToString, True)(0)
        If tb IsNot Nothing Then
            tb.Text = item.Text
        End If

        i += 1
    Next
End Sub

更新:

这更能防错,但这个例程只有在你的文本框被命名为TextBox1TextBox2TextBox3等时才有效:

Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    Dim item As ListViewItem = Nothing
    Dim found() As Control = Nothing
    Dim tb As TextBox = Nothing
    Dim i As Integer = 0

    For Each item In ListView1.SelectedItems
        found = Me.Controls.Find("TextBox" & i.ToString, True)

        If found.Length > 0 Then
            tb = TryCast(found(0), TextBox)
        Else
            tb = Nothing
        End If

        If tb IsNot Nothing Then
            tb.Text = item.Text
        End If

        i += 1
    Next
End Sub

更新:

好的,感谢屏幕截图,我假设您的ListView.MultiSelect = False, 所以一次只能选择一项。鉴于此,只要正确命名文本框和 ListView 列,以下内容应该可以工作:

Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    Dim item As ListViewItem = Nothing
    If ListView1.SelectedItems.Count = 1 Then
        item = ListView1.SelectedItems(0)

        txtCardNumber.Text = item.SubItems("CARD NUMBER")
        txtCardExpirationMonth.Text = item.SubItems("EXP MO")
        txtCardExpirationYear.Text = item.SubItems("EXP YEAR")
    End If
End Sub
于 2012-10-11T10:20:10.417 回答
0

你好伙计们/姑娘们,

在皮特的大力帮助下,我能够修改建议的答案,并实现了所需的解决方案。为了防止显示水平滚动条,我没有向 Listview 添加列(来自设计器)。相反,我以编程方式将这些字段添加到 Listview 中——这样它们就可供选择了。

我遇到的主要麻烦是找出字段的索引号。我不得不调试几次才能弄清楚数字是什么 - 所以如果有人知道更好的方法,请分享。

这是我使用的两个代码(感谢皮特):

    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"))
                    .Add(dbTable.Rows(i)("ccCode"))
                    .Add(dbTable.Rows(i)("ccType"))
                    .Add(dbTable.Rows(i)("ccAuthorizedUseStart"))
                    .Add(dbTable.Rows(i)("ccAuthorizedUseEnd"))
                    .Add(dbTable.Rows(i)("nameCOMPANY"))
                    .Add(dbTable.Rows(i)("nameSALUTATION"))
                    .Add(dbTable.Rows(i)("nameLAST"))
                    .Add(dbTable.Rows(i)("nameFIRST"))
                End With
            End With
        Next
        If dbTable.Rows.Count = 0 Then
            LabelNoCard.Visible = True
            LabelNoCard.Focus()
            TextBoxAccount.Focus()
            Me.Refresh()
        Else
            If dbTable.Rows.Count > 1 Then
                LabelNoCard.Visible = False
                LabelMultipleCards.Visible = True
                ListViewCard.Visible = True
                Me.Refresh()
            End If
        End If
    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

这是第二个:

    Private Sub ListViewCard_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListViewCard.SelectedIndexChanged

    GroupBox2.Visible = True
    Dim item As ListViewItem = Nothing
    If ListViewCard.SelectedItems.Count = 1 Then
        item = ListViewCard.SelectedItems(0)

        TextBoxCustomer.Text = item.SubItems(8).Text
        TextBoxLastName.Text = item.SubItems(10).Text
        TextBoxFirstName.Text = item.SubItems(11).Text
        ComboBoxSalutation.Text = item.SubItems(9).Text
        ComboBoxCardType.Text = item.SubItems(5).Text
        TextBoxCard.Text = item.SubItems(1).Text
        TextBoxExpireMonth.Text = item.SubItems(2).Text
        TextBoxExpireYear.Text = item.SubItems(3).Text
        TextBoxCVV2.Text = item.SubItems(4).Text
        DateTimePickerStartDate.Text = item.SubItems(6).Text
        DateTimePickerEndDate.Text = item.SubItems(7).Text

    End If
    End Sub
于 2012-10-11T17:14:37.680 回答