TextBox Databinding and Triggered Event (Very Urgent!)

A

Ahmad A. Rahman

Hi Wizards,

I have a data set and I bound it to 2 combobox (say ComboBox1 and ComboBox2)
using ComboBox1.DataSource = MyDataSet and it worked well. That also means
that when I select an item from ComboBox1, then SelectedItem in ComboBox2
will also changed respectively, as well.

However, the problem is when I tried to bind the same dataset to a TextBox
using:

TextBox1.DataBinding.Add(new Binding("Text", MyDataSet.Tables["Table1"],
"MyDataColumn"));

It happened to be that, when selectedItem is changed in ComboBox1, the data
in TextBox1.Text does not changed. Is there any other codes/method/event
that I missed? Please note that the DataTable in combobox1 and TextBox1 is
the same. The Text in TextBox should have changed as well, right?

Please help!
 
M

Miha Markic

Hi Ahmad,

What is your code for combobox binding?
I think your problem is that you use different format for binding combos and
textbox thus two bindingmanagers are created and not synchronized.
 
A

Ahmad A. Rahman

Hi,

Here's a few lines from the codes:

//-- code start --//
//I have a combobox ComboBox1 and I've already set the DisplayMember and
ValueMember at design time.
// businesslogic return a dataset with a table and that table name is
MyTable
// I set the DisplayMember = MyTable.TestKey and ValueMember =
MyTable.TestKey at design time

DataSet ds = BusinessLogic.Execute("select TestKey, TestDesc from Test",
"MyTable");

ComboBox1.DataSource = ds;

// now I want to bind the data to the TextBox.Text
TextBox1.DataBinding.Add(new Binding("Text", ds.Tables["MyTable"],
"TestDesc");
//-- code end --//

So, I supposed, when the SelectedItem in ComboBox1 is changed, I thought
data in TextBox1.Text will also change. But that didn't happened.

So, what is the problem here?

Thanx.
 
M

Miha Markic

Hi Ahmad,

I think your problem lies in fact that once you are using ds as datasource
while for the textbox you are using the table.
Try this:

ComboBox1.DataSource = ds.Tables["MyTable"];

Here is my code that works:
//

// comboBox1

//

this.comboBox1.DataSource = this.dataTable1;

this.comboBox1.DisplayMember = "Column1";

this.comboBox1.ValueMember = "Column1";

//

// comboBox2

//

this.comboBox2.DataSource = this.dataTable1;

this.comboBox2.DisplayMember = "Column1";

this.comboBox2.ValueMember = "Column1";

//

// textBox1

//

this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.dataTable1, "Column1"));
 
A

Ahmad A. Rahman

You mean, I should use a DataTable that does not belong to any DataSet?
because refering to your codes, the only difference between my codes and
yours is that, my codes used a datatable in a dataset while yours is not.

Your codes working means that, when you change the SelectedItem in
ComboBox1, the TextBox.Text value also changed as well? Is it?

Why is it not working on mine?

Miha Markic said:
Hi Ahmad,

I think your problem lies in fact that once you are using ds as datasource
while for the textbox you are using the table.
Try this:

ComboBox1.DataSource = ds.Tables["MyTable"];

Here is my code that works:
//

// comboBox1

//

this.comboBox1.DataSource = this.dataTable1;

this.comboBox1.DisplayMember = "Column1";

this.comboBox1.ValueMember = "Column1";

//

// comboBox2

//

this.comboBox2.DataSource = this.dataTable1;

this.comboBox2.DisplayMember = "Column1";

this.comboBox2.ValueMember = "Column1";

//

// textBox1

//

this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.dataTable1, "Column1"));


--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

Ahmad A. Rahman said:
Hi,

Here's a few lines from the codes:

//-- code start --//
//I have a combobox ComboBox1 and I've already set the DisplayMember and
ValueMember at design time.
// businesslogic return a dataset with a table and that table name is
MyTable
// I set the DisplayMember = MyTable.TestKey and ValueMember =
MyTable.TestKey at design time

DataSet ds = BusinessLogic.Execute("select TestKey, TestDesc from Test",
"MyTable");

ComboBox1.DataSource = ds;

// now I want to bind the data to the TextBox.Text
TextBox1.DataBinding.Add(new Binding("Text", ds.Tables["MyTable"],
"TestDesc");
//-- code end --//

So, I supposed, when the SelectedItem in ComboBox1 is changed, I thought
data in TextBox1.Text will also change. But that didn't happened.

So, what is the problem here?

Thanx.
 
M

Miha Markic

Ahmad A. Rahman said:
You mean, I should use a DataTable that does not belong to any DataSet?
because refering to your codes, the only difference between my codes and
yours is that, my codes used a datatable in a dataset while yours is not.

Why do you think my is not in a dataset?
It is.
Your codes working means that, when you change the SelectedItem in
ComboBox1, the TextBox.Text value also changed as well? Is it?
Yup.

Why is it not working on mine?

Because the datasource matters - if you want to synchronize it you should
use the same.
You can't use ds for combobox and ds.Tables["MyTable"] for text box -
because the first is a dataset and the second is a datatable.
 
A

Ahmad A. Rahman

Now it's working!!!! Obviously the datasource is the keypoint to this
problem.

Thanks a lot Miha. You really are a great help.

-cheers-
 

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