Bind 2 or More Windows Controls To One Datasource
5
MovingParts
so you executed a query that took .5 second to execute and you want to bind that to a windows control. next you decide that you will need to have two controls bound to the same datasource. not a problem, except when you do this:
control1.datasource = datatable
control2.datasource = datatable
you notice that each control mirrors the other and you cannot pick two seperate and distinct values.
one way to solve this is to create two datatables and execute the query twice. unfortunately this now doubles your query time. bad idea.
here is the elegant solution...
control1.datasource = datatable
control2.datasource = datatable
you notice that each control mirrors the other and you cannot pick two seperate and distinct values.
one way to solve this is to create two datatables and execute the query twice. unfortunately this now doubles your query time. bad idea.
here is the elegant solution...
Private Sub Foo()
'THE PURPOSE OF THIS SUB IS BIND ONE DATASOURCE TO TWO DIFFERENT WINDOWS COMPONENTS
'THE SECRET IS TO USE THE New Windows.Forms.BindingContext COMMAND
'CREATE A DATATABLE THAT WILL HOLD THE NAMES OF ALL 50 US STATES
Dim objDT As New Data.DataTable
objDT = GetAll50States() 'THE QUERY THAT TAKES A LONG TIME TO EXECUTE
'Fill ShipFrom Combo
cmbShipFrom.BindingContext = New Windows.Forms.BindingContext
cmbShipFrom.DataSource = objDT
cmbShipFrom.DisplayMember = "FullName"
cmbShipFrom.ValueMember = "PostalAbbreviation"
'Fill ShipTo Combo
cmbShipTo.BindingContext = New Windows.Forms.BindingContext
cmbShipTo.DataSource = objDT
cmbShipTo.DisplayMember = "FullName"
cmbShipTo.ValueMember = "PostalAbbreviation"
End Sub






There are currently no comments for this snippet.