Change value in bound ctrl when value of another bound ctrl change

G

Guest

I have been developing WinForms apps in .NET for several years and have never
discovered a good solution to this newbish problem. I frequently encounter a
situation where I want to change the value of a control when the user changes
the value of some other control and both controls are bound to the same row
in a DataTable.

For example, say I have a combobox of statuses and a textbox for comments
but comments are not applicable for certain statuses. When the user selects
one of these statuses I want to set the text of the textbox to an empty
string. So I add a handler to the combobox's SelectedValueChanged event and
set the TextBox's Text property to an empty string.

Doing this seems to cause unpredictable behavior. Sometimes I get an empty
textbox but the combobox is changed back to its original value and other
times the textbox is not cleared. I assume one change gets pushed to the
dataset and triggers a refresh of the controls from the dataset but I'm not
really sure. I have worked around this by clearing the textbox when the
combobox loses focus but users are never happy with this solution. Having
encountered this problem again I thought I would see if anyone can tell me
the "right" way to do this.
 
D

Dmytro Lapshyn [MVP]

Hi Jim,
I assume one change gets pushed to the
dataset and triggers a refresh of the controls from the dataset but I'm
not
really sure.

That's what databinding really does behind the scenes.
Therefore, here's what I would do. First, I would create a typed dataset
with two related tables - the first is the main table with data and the
second is a lookup table mapping status codes to their descriptions. A
relation must be established between the two using the status code field as
the foreign key.

Then, I'd bind the combo box to the dataset, specifying the name of the main
table followed by a dot and by the column name as the datamember. For
example:

comboBox.DataSource = theDataSet;
comboBox.DisplayMember = "MainTable.StatusCaption";
comboBox.ValueMember = "MainTable.StatusCode";

Second, I'd bind the text box to the dataset, specifying the name of the
main table followed by a dot and by the name of the relation and then by
another dot and finally by the name of the description column as the
datamember. For example:

textBox.DataBindings.Add("Text", theDataSet,
"MainTable.FK_Main_Lookup.StatusDescription");

Hopefully, in this scenario, changing the status code should trigger the
textbox' binding to automatically update the description.
 
G

Guest

Hi Dmytro,

Thanks for replying. I'll whip up a test of your code in a minute to see if
it will work for the case I described, but what if I change the example so
that the ComboBox becomes a CheckBox bound to a boolean column in the same
DataTable that the TextBox is bound to. When a user Checks the Box I want to
clear the TextBox. In this scenario there is only one table so there would
be no relationship.

I'll post the results of my test of your suggestion shortly.
 
G

Guest

OK. After more thorough investigation I have a much better definition of the
problem I was having. The problem as I described it does not exist.
However, what if you display the table information on your form using a
readonly DataGrid and you want the grid to update immediately when the user
makes a change in the control. I was updating the grid by call
EndCurrentEdit on the BindingManager in the corresponding "changed" event and
this is a no-no.

I can handle this by setting the Cells' values in the grid directly but the
code seems odd. When you change the ComboBox set the TextBox.Text to "" and
the corresponding cell value to match and set the Cell corresponding to the
ComboBox to the newly selected value. Is there any other way to handle this?
 

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