Binding data to controls?

M

Max

Hello,

I made a windows form with a combo box and 4 text boxes. All 5 objects should get their data from a data set which is populated
in the form load method. The combo box has item ids. When the users selects an item from the combo box I'd like the 4 text boxes to
get populated with the corresponding item information from the same dataset table row that the combo box is pulling it's info from.
Is there an easy way of doing this besides writing code to query the database based on the combo box id? (sample code will be
appreciated)

Cheers,
Max.
 
N

Nicholas Paldino [.NET/C# MVP]

Max,

As long as you are binding to the same data table, then you should have
no problem with the textboxes changing when you change the current record in
the combobox. Just make sure that the data table that you bind everything
to has the value that you show in the combobox, as well as the other values
you would show in the textboxes.
 
M

Max

Can you give me an example because I can't seem to get this working. This is what I have:

cb_myCombo.DataSource = ds_myDS.Tables["X"];
cb_myCombo.DisplayMember = "A";
txt_myText1.DataBindings.Add("Text",ds_myDS,"Y");
txt_myText2.DataBindings.Add("Text",ds_myDS,"Z");
.....

Max




Nicholas Paldino said:
Max,

As long as you are binding to the same data table, then you should have
no problem with the textboxes changing when you change the current record in
the combobox. Just make sure that the data table that you bind everything
to has the value that you show in the combobox, as well as the other values
you would show in the textboxes.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Max said:
Hello,

I made a windows form with a combo box and 4 text boxes. All 5 objects
should get their data from a data set which is populated in the form load
method. The combo box has item ids. When the users selects an item from
the combo box I'd like the 4 text boxes to get populated with the
corresponding item information from the same dataset table row that the
combo box is pulling it's info from. Is there an easy way of doing this
besides writing code to query the database based on the combo box id?
(sample code will be appreciated)

Cheers,
Max.
 
N

Nicholas Paldino [.NET/C# MVP]

Max,

The reason it doesn't work is because you are essentially binding to two
different data sources.

What makes a binding context unique is a combination of the data source
and the data member, both of which have to be the same to share the same
binding context. For your combo box, you have this:

DataSource - ds_myDS.Tables["X"]
DataMember - null

For the textboxes, this is your data source:

DataSource - ds_myDS
DataMember - "X"

Now, while they will both yield the same DataTable, the binding
infrastructure looks at them as two separate binding contexts, and that's
why they don't change.

You need to bind to the same thing, so you need to do this:

cb_myCombo.DataSource = ds_myDS;
cb_myCombo.DataMember = "X";
cb_myCombo.DisplayMember = "A";
txt_myText1.DataBindings.Add("Text",ds_myDS,"Y");
txt_myText2.DataBindings.Add("Text",ds_myDS,"Z");

Or do this:

cb_myCombo.DataSource = ds_myDS.Tables["X"];
cb_myCombo.DisplayMember = "A";
txt_myText1.DataBindings.Add("Text",ds_myDS.Tables["Y"], null);
txt_myText2.DataBindings.Add("Text",ds_myDS.Tables["Z"], null);


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Max said:
Can you give me an example because I can't seem to get this working. This
is what I have:

cb_myCombo.DataSource = ds_myDS.Tables["X"];
cb_myCombo.DisplayMember = "A";
txt_myText1.DataBindings.Add("Text",ds_myDS,"Y");
txt_myText2.DataBindings.Add("Text",ds_myDS,"Z");
....

Max




Nicholas Paldino said:
Max,

As long as you are binding to the same data table, then you should
have no problem with the textboxes changing when you change the current
record in the combobox. Just make sure that the data table that you bind
everything to has the value that you show in the combobox, as well as the
other values you would show in the textboxes.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Max said:
Hello,

I made a windows form with a combo box and 4 text boxes. All 5 objects
should get their data from a data set which is populated in the form
load method. The combo box has item ids. When the users selects an item
from the combo box I'd like the 4 text boxes to get populated with the
corresponding item information from the same dataset table row that the
combo box is pulling it's info from. Is there an easy way of doing this
besides writing code to query the database based on the combo box id?
(sample code will be appreciated)

Cheers,
Max.
 

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