当我 select 来自组合框的项目时如何从数据库填充文本框
How to populate a textbox from database when i select an item from combo box
当我 select 来自组合框的项目(即组合框)填充数据库列 1 时,我如何填充文本框我的数据库列 2
这就是我想要发生的事情:
当我 select 组合框中的项目时,文本框将自动填充数据。
This is what i want to happen
这是我的 Table:
我想要的是当我 select 客户名称时,文本框将由客户联系人自动填写。
My table
**这是我试过的**
Private Sub clientNameText_SelectedIndexChanged(sender As Object, e As EventArgs) Handles clientNameText.SelectedIndexChanged, clientNameText.TextChanged
Try
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nikko Jaze Fabellon\Documents\ASRASIM.accdb")
connection.Open()
Dim reader As OleDbDataReader
Dim cmdText = "select [Contact Person] from ServiceRecords where [ClientName] =' " & clientNameText.Text & "'"
dataAdapter = New OleDbDataAdapter(cmdText, connection)
Dim dtable As New DataTable
dataAdapter.Fill(dtable)
reader = dataAdapter.SelectCommand.ExecuteReader
While reader.Read
cPersonText.Text = reader.GetInt32("Contact Person")
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
connection.Close()
End Sub
只需将 TextBox
绑定到相同的数据源,例如
myTextBox.DataBindings.Add("Text", ds.Tables("Clients"), "ColumnName")
编辑:这一次,请实际按照我的建议进行。您需要查询来检索要显示的所有列。您 Fill
使用数据适配器 DataTable
然后将 DataTable
绑定到您的控件。就是这样,就是这样。例如
Dim adapter As New OleDbDataAdapter("SELECT ID, Name, Description FROM MyTable", "connection string here")
Dim table As New DataTable
adapter.Fill(table)
With myComboBox
.ValueMember = "ID"
.DisplayMember = "Name"
.DataSource = table
End With
myTextBox.DataBindings.Add("Text", myDataTable, "Description")
这就是你需要的。在 ComboBox
中进行选择,TextBox
将显示正确的数据。
当我 select 来自组合框的项目(即组合框)填充数据库列 1 时,我如何填充文本框我的数据库列 2
这就是我想要发生的事情: 当我 select 组合框中的项目时,文本框将自动填充数据。
This is what i want to happen
这是我的 Table: 我想要的是当我 select 客户名称时,文本框将由客户联系人自动填写。
My table
**这是我试过的**
Private Sub clientNameText_SelectedIndexChanged(sender As Object, e As EventArgs) Handles clientNameText.SelectedIndexChanged, clientNameText.TextChanged
Try
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nikko Jaze Fabellon\Documents\ASRASIM.accdb")
connection.Open()
Dim reader As OleDbDataReader
Dim cmdText = "select [Contact Person] from ServiceRecords where [ClientName] =' " & clientNameText.Text & "'"
dataAdapter = New OleDbDataAdapter(cmdText, connection)
Dim dtable As New DataTable
dataAdapter.Fill(dtable)
reader = dataAdapter.SelectCommand.ExecuteReader
While reader.Read
cPersonText.Text = reader.GetInt32("Contact Person")
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
connection.Close()
End Sub
只需将 TextBox
绑定到相同的数据源,例如
myTextBox.DataBindings.Add("Text", ds.Tables("Clients"), "ColumnName")
编辑:这一次,请实际按照我的建议进行。您需要查询来检索要显示的所有列。您 Fill
使用数据适配器 DataTable
然后将 DataTable
绑定到您的控件。就是这样,就是这样。例如
Dim adapter As New OleDbDataAdapter("SELECT ID, Name, Description FROM MyTable", "connection string here")
Dim table As New DataTable
adapter.Fill(table)
With myComboBox
.ValueMember = "ID"
.DisplayMember = "Name"
.DataSource = table
End With
myTextBox.DataBindings.Add("Text", myDataTable, "Description")
这就是你需要的。在 ComboBox
中进行选择,TextBox
将显示正确的数据。