I have a BusinessBase object called FavoriteColors (not really, this is just a simplified example of my question). It has two properties: FavoriteColorYesterday and FavoriteColorToday. They are both ints, which map to colors in a Color table (1=Red, 2=Blue, etc...).
On my form, I have a BindingSource. The form also has two comboboxes, one each for FavoriteColorYesterday and FavoriteColorToday. Each is bound to the appropriate property. In the form constructor, I create an instance of FavoriteColors (_favoriteColors) and set it as the DataSource of the BindingSource (via the BindUI function as shown in the ProjectTracker example). Everything works fine.
Now I want to make it so that when the user selects a color in the FavoriteColorYesterday combobox, that the same color will automatically be selected in the FavoriteColorToday combox (since the user will usually want the same color in both places, this will save them a little time). I can't figure out how to make this happen.
In my comboBoxFavoriteColorYesterday_SelectedIndexChanged event, I have
if(comboBoxFavoriteColorYesterday.SelectedItem.Text == "Red")
_favoriteColors.FavoriteColorToday = 1;
But this doesn't work. The comboBoxFavoriteColorYesterday doesn't change. The comboBoxFavoriteColorToday does change, but the change doesn't get saved.
I've also tried setting the property on the bindingsource like this: ((FavoriteColors)bindingSourceFavoriteColors).FavoriteColorToday = 1;
I've also tried setting the SelectedIndex property of comboBoxFavoriteColorToday. Nothing works. What am I doing wrong?
You should perform the change within your BO. Override OnPropertyChanged:
protected override void OnPropertyChanged(string propertyName)
{
if (propertyname.Equals("YesterdaysColor")
{
TodaysColor = YesterdaysColor;
}
...
}
I also assume you are using WinForms in which case if I remember well you will need a BindingSourceRefresh otherwise the value will be updated in your BO but the change will not be reflected on your UI.
HTH
Thanks, that works. No BindingSourceRefresh needed.
Hi,
You shouldn't need the BindingSourceRefresh but I'd rather suspect that the sequence for UnBinding bindingsources is not correct.
Try to set a breakpoint on the property setter to see if the value is "destroyed" from the UI after you have set the BO value.
Copyright (c) Marimer LLC