I wish to go to the next row of SQL Data depending on Combobox selection
Once you have your data reader, create a DataTable
and call its Load
method to load all the data. You can then bind the DataTable
to a BindingSource
and the BindingSource
to your TextBoxes
. You can then call MoveNext
to advance through the data.
myDataTable.Load(myDataReader)
myBindingSource.DataSource = myDataTable
myTextBox.DataBindings.Add("Text", myBindingSource, "MyColumn")
EDIT:
After doing some reading and testing based on Joel Coehoorn's comment below, I have learned that, while you cannot bind a data reader directly to controls in Windows Forms, you can bind one to a BindingSource
and it will generate an IBindingList(Of DataRecordInternal)
, which is an IList
and thus satisfies the requirements for WinForms binding. A DataRecordInternal
can be indexed by column name or ordinal in the same way a DataRow
or DataRowView
can, so they can be treated in much the same way for the purposes of binding, e.g.
Using connection As New SqlConnection("connection string here"),
command As New SqlCommand("SELECT * FROM MyTable", connection)
connection.Open()
Using reader = command.ExecuteReader()
BindingSource1.DataSource = reader
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "Id"
ComboBox1.DataSource = BindingSource1
TextBox1.DataBindings.Add("Text", BindingSource1, "Description")
End Using
End Using
Either way