Bind / Synchronize a comboBox to a filed the current record

D

dbuchanan

Hello,

I have the controls on my form display the values of the currently
selected row of the dataGrid. There are a few fields that I have in my
data table where I have the user select a value from a comboBox. The
comboBox is populated from a lookup table in my database. The comboBox
displays the range of choices and when the user selects one the
comboBox stores the appropriate ID field in the field of my data table.


The above is easy enough, but how do I wire up the dataGrid/comboBox so
that as the user moves through the records in the dataGrid that the
comboBox will display the proper value for each row?

Thank you,
- Doug
 
C

Cor Ligthert

Dbuchan,

I have changed an older sample from me what was terrible quick and dirty.
Although this has maybe as well some errors, is it in my opinion better to
understand. The first part is only building of the tables.

'\\datagrid sample with combobox outside
'needs a datagrid, a combobox and two buttons on a form
'to start do the button create ds and cancel and a dataset will be created
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet("Test")
Dim dtName As New DataTable("Names")
Dim dcIdName As New DataColumn("IdName")
Dim dcName As New DataColumn("Name")
Dim dcCountryN As New DataColumn("Country")
dtName.Columns.Add(dcIdName)
dtName.Columns.Add(dcName)
dtName.Columns.Add(dcCountryN)
ds.Tables.Add(dtName)
dtName.LoadDataRow(New Object() {"0", "Herfried K. Wagner", "Austria"},
True)
dtName.LoadDataRow(New Object() {"1", "Cor Ligthert", "Holland"},
True)
dtName.LoadDataRow(New Object() {"2", "Armin Zingler", "Germany"},
True)
dtName.LoadDataRow(New Object() {"3", "Ken Tucker", "Florida"},
True)
dtName.LoadDataRow(New Object() {"4", "Jay B. Harlow", "New York"},
True)
dtName.LoadDataRow(New Object() {"5", "Terry Burns", "England"},
True)
Dim dtCountry As New DataTable("Countries")
dtCountry.Columns.Add("Country")
dtCountry.Columns.Add("IdCountry")
dtCountry.LoadDataRow(New Object() {"0", "Austria"}, True)
dtCountry.LoadDataRow(New Object() {"1", "Germany"}, True)
dtCountry.LoadDataRow(New Object() {"2", "Holland"}, True)
dtCountry.LoadDataRow(New Object() {"3", "Georgia"}, True)
dtCountry.LoadDataRow(New Object() {"4", "Florida"}, True)
dtCountry.LoadDataRow(New Object() {"5", "England"}, True)
ds.Tables.Add(dtCountry)
Dim dv As DataView = New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
ComboBox1.DataSource = ds.Tables(1).DefaultView
ComboBox1.ValueMember = "IdCountry"
ComboBox1.DisplayMember = "Country"
ComboBox1.DataBindings.Add(New Binding("SelectedValue", dv,
"Country"))
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender _
As Object, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
Me.BindingContext(DataGrid1.DataSource).EndCurrentEdit()
End Sub
End Class
///

I hope this helps a little bit?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top