bindingcontext question

J

JohnR

I have a question on bindingcontext, bindings, and
controlbindingscollection. I understand the very basic idea, and I think I
should be able to do this, but I'm not sure how.

First problem: I have a combobox and a textbox on a form. I also have a
"Customers" table loaded into a dataset. I set the combobox.datasource to
the table in the dataset, and add a binding to the text property of the
combobox for the 'Name' field. I also add a binding to the textbox.text
property for 'Address' like this:
combobox.databindings.add(new binding("text", ds.tables("customer"),
"Name"))
combobox.datasource = ds.tables("customer")
textbox.databindings.add(new binding("text", ds.tables("customer"),
"Address"))

When run the combobox has the first name in the table, and the proper
address. I can use the dropdown to select a new name, but when I do the
address in the textbox does not change. Question: what do I need to do to
get the textbox to display the correct address after selecting a name in
the combobox?

Second Situation: I would like to present data in 2 different ways, each
presentation would be on a different 'tab' . The first presentation would
basically be what I said before.. a combobox for selecting a record, and a
bunch of textboxes that list the info for the record selected in the
combobox. The second tab screen would be in the form of a datagrid that
lists all of the records. I would do a datagrid.datasource =
ds.tables("customer"). Here is where I'm a little confused... how can I
link the 2 views of the data. That is, when on the data grid, if the user
selects a row or a cell, and then switches back to the 1st view, the record
he chose in the datagrid is the record that will be displayed in the
combobox and textboxes. Likewise, if the user selects a record in the
combobox and switches to the datagrid tab, the row selected in the grid will
be the record he chose in the combobox.

Is there some way to do this synchronization automatically via the
currencymanager or databindings? Or is it something that needs to be done
manually.

Thanks for any advise... I'm still learning!

John
 
C

Cor Ligthert [MVP]

JohnR,

I never succeeded in binding the text in a combobox.

If you succeed (completely) give than a message.

This is one of the few problems that I have asked help for in this newsgroup
and the newsgroup vb.controls. I seldom got an answer, some of them did not
even get a reply and the replies did not help me.

In some situations it will help if you use the selectedvalue property.
However, as well not in all. It depend as well what event you use.

Therefore don't expect to much help here in this binding question about the
combobox. (You can use dummy textboxes than it works)

In my opinion it can really act very strange.

Cor
 
B

Bart Mermuys

Hi,

JohnR said:
I have a question on bindingcontext, bindings, and
controlbindingscollection. I understand the very basic idea, and I think I
should be able to do this, but I'm not sure how.

First problem: I have a combobox and a textbox on a form. I also have a
"Customers" table loaded into a dataset. I set the combobox.datasource to
the table in the dataset, and add a binding to the text property of the
combobox for the 'Name' field. I also add a binding to the textbox.text
property for 'Address' like this:
combobox.databindings.add(new binding("text", ds.tables("customer"),
"Name"))
combobox.datasource = ds.tables("customer")
textbox.databindings.add(new binding("text", ds.tables("customer"),
"Address"))

When run the combobox has the first name in the table, and the proper
address. I can use the dropdown to select a new name, but when I do the
address in the textbox does not change. Question: what do I need to do to
get the textbox to display the correct address after selecting a name in
the combobox?

If i understand i right, you don't need to bind the Text property of the
ComboBox but use the DisplayMember instead. You also have to bind the
ComboBox and TextBox to the same DataSource (either DataSet or DataTable,
don't mix them).

eg:

combobox.DataSource = ds.Tables("customer")
combobox.DisplayMember = "Name"

textbox.DataBindings.Add( "Text", ds.Tables("customer"), "Address" )

Both are bound to the same DataTable (customer part of the DataSet).
Therefore they will share the same CurrencyManager and they will navigate
together.
Second Situation: I would like to present data in 2 different ways, each
presentation would be on a different 'tab' . The first presentation would
basically be what I said before.. a combobox for selecting a record, and
a bunch of textboxes that list the info for the record selected in the
combobox. The second tab screen would be in the form of a datagrid that
lists all of the records. I would do a datagrid.datasource =
ds.tables("customer"). Here is where I'm a little confused... how can I
link the 2 views of the data. That is, when on the data grid, if the user
selects a row or a cell, and then switches back to the 1st view, the
record he chose in the datagrid is the record that will be displayed in
the combobox and textboxes. Likewise, if the user selects a record in
the combobox and switches to the datagrid tab, the row selected in the
grid will be the record he chose in the combobox.

Same here, just bind the DataGrid to the same DataSource, remember we used a
DataTable for the TextBox and ComboBox, so we have to do the same for the
DataGrid :

datagrid.DataSource = ds.Tables("customer")
Is there some way to do this synchronization automatically via the
currencymanager or databindings? Or is it something that needs to be done
manually.

If you bind different Controls to the (exact) same DataSource and the
Controls have the same BindingContext (which is the default within a Form)
then they will share the same CurrencyManager and navigate together.

HTH,
Greetings
 

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