Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a BindingSource current record to null?

I have a capture form for a Works Order, and it has a CustomerBindingSource and a WorksOrderBindingSource control. Most edit fields are bound to the WorksOrderBindingSource, with a ComboBox whose list is bound to the CustomerBindingSource, and its SelectedValue is bound to the CustomerId field in the WorksOrderBindingSource. This is all very routine and standard, no funnies here.

Then, I also have some textbox fields in which I use to show properties of the currently selected customer, for the currently edited works order. I have bound these fields to the CustomerBindingSource as well. When a customer is selected, these fields show properties of that customer as expected.

My problem is when I want to use the form to capture a new works order. I instantiate a new WorksOrder object, with CustomerId == null and bind it to the WorksOrderBindingSource. I have no object in the CustomerBindingSource with an Id == null, so, as expected, the dropdown combobox is blank, but, the CustomerBindingSource.Current property points to the first Customer object in that datasource. The customer linked display fields show values for that customer, while no customer has been selected yet.

The only workaround for this that is apparent to me seems clumsy. In it, I have two Customer typed binding sources, one for a selected customer, and to populate the customer display fields, and another merely for populating the customer dropdown. Then, I have to handle a selection event, and only if a customer is selected, then find that customer in the binding source for the display fields, and if none selected, set the datasource for the display fields to null. This feels terribly clumsy. Is there any other way of achieving what I want?

like image 902
ProfK Avatar asked Sep 27 '12 06:09

ProfK


1 Answers

I found this topic with exactly my problem but without satisfying answer. I know its an old topic but alah..

I ended up with a working solution: I added a [PositionChanged] event to my bindingsource (would be your CustomerBindingSource).

        private void CustomerBindingSource_PositionChanged(object sender, EventArgs e)
    {
        if(<yourCombobox>.SelectedIndex==-1)
        {
            CustomerBindingSource.SuspendBinding();
        }
        else
        {
            CustomerBindingSource.ResumeBinding();
        }
    }
like image 57
Remco Spek Avatar answered Sep 26 '22 19:09

Remco Spek