How can I set a BindingSource current record to null?
Asked Answered
K

2

7

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?

Kingcraft answered 27/9, 2012 at 6:28 Comment(2)
Why not adding a "please-select-a-customer" item to the binding source?Cheapskate
What about to force the user to save current edits so that a valid CustomerID is assigned to your NewRowBekki
K
2

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();
        }
    }
Kor answered 29/11, 2017 at 11:54 Comment(0)
A
0

What I use to "clear" a BindingSource is to simply set its DataSource like this:

CustomerBindingSource.DataSource = typeof(Customer);

Hope this helps.

EDIT:

For clarity, when you set the BindingSource.DataSource property as described, there's nothing preventing you to reassign the original data source at a later point:

//Retrieve customers from database
List<Customer> Customers = WhatEverCallToDB();
CustomerBindingSource.DataSource = Customers;

...

//Later we need to blank the Customer fields on the Windows Form
CustomerBindingSource.DataSource = typeof(Customer);

...

//Then again at a later point we can restore the BindingSource:
CustomerBindingSource.DataSource = Customers;

...
Apogeotropism answered 14/10, 2012 at 4:35 Comment(6)
I don't want to clear the binding source. I want it to be known that no one record in the binding source is selected, or 'active'.Kingcraft
I thought you meant that when you create a new WorkOrder, you want to "blank" out the fields related to the Customer. If so, then my method will work, but if I misunderstand your use case, could you clarify ?Apogeotropism
I do want to blank out the fields related to the customer, but I don't want to lose the set of customer records in the CustomerBindingSource.DataSource. I just want none of those records selected.Kingcraft
Ok, well, in your code you must already have a reference to your data object, the one holding the customer records, such as a DataTable or whatever. So, with the technique I described, you don't lose that reference. You can still reassign the CustomerBindingSource.DataSource = MyCustomerDataObject at a later point.Apogeotropism
When do I set DataSource = typeof(Customer)? When need the selection cleared is when I add a new WorksOrder, but then the user needs to have customers to select from.Kingcraft
I see. I guess I didn't understand your workflow properly, or I was focusing only on clearing the form fields. Using two BindingSources is an option. Another option is to simply create the new WorkOrder for the currently selected Customer by default, allowing it to be changed if needed. Another option would be to have a "dummy" Customer with empty fields used when creating a WorkOrder. There's more than one way to skin a cat :-)Apogeotropism

© 2022 - 2024 — McMap. All rights reserved.