Just to make sure I understand what you're going for:
You have a grid and a textbox. You want to be able
to click on a row in the grid and have it show one
of the fields in the textbox with the value corresponding
to the selected row.
Okay, you asked for it, here's the giant example. I got
this from Brian Noyes' book on Data Binding.
For this example, I have a CustomersDataSet defined.
It contains the Customers table in the Northwind database.
I have a Grid that is going to be bound to the CustomersDataSet
called CustomersDataGridView.
I have textboxes for CustomerID, ContactName, CompanyName,
and ContactPhone.
I also have a combobox for ContactNames.
I have ONE binding source: CustomersBindingSource.
When you change the row selected in the grid, it populates
the textboxes with the corresponding data. The combobox
for ContactNames shows the corresponding contact name.
I also have buttons on the screen for moving through the
records, and a textbox for the record# (position). If you
move through the records that way, it changes the position
in the grid, and changes the textboxes and combobox
accordingly.
If they change the ContactName in the combobox, it
positions the grid at the record that matches it, and
changes the textboxes accordingly.
This runs against NorthWind, so you could theoretically
hook this up and run it if you have that database, if you
set up the screen with the same controls (you should be
able to figure out their names by the code below).
---------------------------------------------------------------------
Private Sub MainForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Set up event handlers for connector position changed.
AddHandler CustomersBindingSource.PositionChanged, _
AddressOf OnPositionChanged
AddHandler PositionTextBox.TextChanged, _
AddressOf OnPositionTextChanged
'Add event handlers for the buttons; you could do this
' by setting the properties on the buttons, but I'd
' rather do it specifically
AddHandler MoveFirstButton.Click, AddressOf OnFirstRecord
AddHandler MovePreviousButton.Click, AddressOf OnPreviousRecord
AddHandler MoveNextButton.Click, AddressOf OnNextRecord
AddHandler MoveLastButton.Click, AddressOf OnLastRecord
'Set up data bindings.
'First, populate the dataset.
Dim nwData As CustomersDataSet = CustomersDataSet.GetCustomers()
'Set the data source for the binding source to the dataset.
CustomersBindingSource.DataSource = nwData
'Add this so it knows which table to use .
CustomersBindingSource.DataMember = "Customers"
'Set the data source for the grid to the customers binding source.
CustomersDataGridView.DataSource = CustomersBindingSource
'Add the bindings for the controls on the form.
AddTextBoxDataBindings()
AddComboBoxDataBindings()
End Sub
Private Sub AddTextBoxDataBindings()
'Bind each text box to the appropriate field
' in the CustomersBindingSource
CustomerIDTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "CustomerID")
ContactNameTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "ContactName")
CompanyNameTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "CompanyName")
ContactPhoneTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "Phone")
End Sub
Private Sub AddComboBoxDataBindings()
ContactsComboBox.DataSource = CustomersBindingSource
ContactsComboBox.DisplayMember = "ContactName"
ContactsComboBox.ValueMember = "CustomerID"
End Sub
Private Sub OnPositionChanged(ByVal sender As Object, _
ByVal e As EventArgs)
PositionTextBox.Text = CustomersBindingSource.Position.ToString
End Sub
Private Sub OnPositionTextChanged(ByVal sender As Object, _
ByVal e As EventArgs)
Dim enteredPos As Integer
'If it parses to an integer successfully, change the position.
Dim success As Boolean = Integer.TryParse(PositionTextBox.Text, _
enteredPos)
If success Then
CustomersBindingSource.Position = enteredPos
End If
End Sub
Private Sub OnFirstRecord(ByVal sender As Object, _
ByVal e As EventArgs)
CustomersBindingSource.MoveFirst()
End Sub
Private Sub OnPreviousRecord(ByVal sender As Object, _
ByVal e As EventArgs)
CustomersBindingSource.MovePrevious()
End Sub
Private Sub OnNextRecord(ByVal sender As Object, _
ByVal e As EventArgs)
CustomersBindingSource.MoveNext()
End Sub
Private Sub OnLastRecord(ByVal sender As Object, _
ByVal e As EventArgs)
CustomersBindingSource.MoveLast()
End Sub