Groups | Blog | Home
all groups > dotnet windows forms databinding > june 2006 >

dotnet windows forms databinding : ComboBox Update problem


Jeronimo Bertran
6/16/2006 2:31:57 PM

I am using a BindingNavigator inside a Form that has some ForeignKey
related combo boxes. I am trying to limit the options available in my
comboBox based on the value of on of the fields in the underlying
datasource row.

For example, my datasource has a field StateID and CityID. StateID is a
foreign key to a State table and CityID is a foreign key to a City table.

My form has a comboState which is bound to StateID and is filled with all
states. I also have a comboCity which is bound to CityID.

When the user navigates through the rows, depending on the value of StatID
I want to fill the comboCity to only include cities for that state and I
need then the current city to be selected in the combo via the DataBinding
for the cityCombo.

I have managed to populate the comboCity with the correct cities by
processing the PositionChanged event and getting the current state from the
bindingSource.Current. My problem is that the comboCity is databound to
CityID before the combo is populated and therefore the correct city is not
selected.

How can I force the combo to perfrom databinding or what is the suggested
procedure to achieve this?

Thanks

v-lliu NO[at]SPAM online.microsoft.com
6/19/2006 12:00:00 AM
Hi Jeronimo,

Thank you for posting.

You have found out the reason why the comboCity didn't show the correct
city by saying that "the comboCity is databound to CityID before the combo
is populated and therefore the correct city is not selected".

To make the comboCity show the correct city, you should add a statement to
assign the correct value to the comboCity's SelectedValue after the code
populating the comboCity in the BindingSource instance's PositionChanged
event handler. The statement is something like below:
this.comboCity.SelectedValue =
((DataRowView)this.customerBindingSource.Current)["CityID"];

However, I recommend you to move the code populating the comboCity and
setting the current value to comboCity into the comboState's
SelectedIndexChanged event handler. The reason is that every time you
navigate through the rows, the BindingSource's PositionChanged event is
raised and the corresponding event handler will be executed, which may not
be necessary. Only if the comboState's selectedIndex changes, the
datasource of the comboCity needs changing.

The following is a sample of placing the processing code in the
comboState's SelectedIndexChanged event handler.

private void comboState_SelectedIndexChanged(object sender, EventArgs e)
{
// remember to add a statement to check whether the SelectedItem is null
before
// processing the SelectedValue of the comboState.
if (comboState.SelectedItem != null)
{
int stateid = (int)this.comboState.SelectedValue;
this.cityTableAdapter.Fill(this.testDataBaseDataSet.City,
stateid);
this.comboCity.SelectedValue =
((DataRowView)this.customerBindingSource.Current)["CityID"];

}
}

Please try my suggestions and let me know the result.

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
Jeronimo Bertran
6/23/2006 10:32:04 PM
Hi Linda,

Thanks for the response.

Your suggestion worked very well,
v-lliu NO[at]SPAM online.microsoft.com
6/26/2006 12:00:00 AM
Hi Jeronimo,

Thanks for your response. I am glad to hear that the problem has been fixed.

If you have any other questions, please don't hesitate to contact us. It's
always our pleasure to be of assistance.

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
Mihai Pascu
7/10/2006 3:51:20 AM
Well, this is a trick, not a solution. He wanted to use the
BindingSource, not the value of combobox .

For example ,in my case, I can't use it, because I display a field ,
which is a friendly text, but I need values from two other fields. So,
how do I do ?
In SelectedIndexChanged() I put BindingSource.CurrencyManager.Refresh().
But when the form loads, an error message appears and says to check if I
don't have an infinite loop.
How can I resolve this ?

Thanks,
Mihai .


AddThis Social Bookmark Button