你试过这个吗?
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
(或类似的控件),然后将其设置为。这将为您创建文本框控件。dbTable
DataSource
DataGrid.EditMode
True
更新:
尝试像这样将您的文本框绑定到您的 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
更新:
这更能防错,但这个例程只有在你的文本框被命名为TextBox1
、TextBox2
、TextBox3
等时才有效:
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