How to set current record BindingSource to null?

I have a capture form for a Works order, and it has a CustomerBindingSource and WorksOrderBindingSource . Most edit fields are bound to WorksOrderBindingSource , with a ComboBox, a list of which is bound to CustomerBindingSource , and its SelectedValue bound to the CustomerId field in WorksOrderBindingSource . This is all very routine and standard, without laughter.

Then I also have text field fields in which I use to show the properties of the currently selected client for the current edited work order. I bound these fields to CustomerBindingSource . When a client is selected, these fields show the properties of that client as expected.

My problem is when I want to use a form to capture a new order of work. I create a new WorksOrder object with CustomerId == null and bind it to WorksOrderBindingSource . I do not have an object in CustomerBindingSource with Id == null , therefore, as expected, the combobox is empty, but the CustomerBindingSource.Current property points to the first Customer object in this data source. The fields associated with the client display the values โ€‹โ€‹for this client, while the client has not yet been selected.

The only workaround for this that seems obvious to me seems awkward. In it, I have two sources of client binding, one for the selected client and filling in the clientโ€™s display fields, and the other for filling out the drop-down list of clients. Then I need to handle the selection event, and only if the client is selected, then find this client in the binding source for the display fields, and if none is selected, set the data source for the display fields to null. It seems awfully awkward. Is there any other way to achieve what I want?

+6
source share
2 answers

I found this topic precisely with my problem, but without satisfying the answer. I know his old theme, but ala ..

In the end, I got a working solution: I added the [PositionChanged] event to my bindingsource (there will be your CustomerBindingSource).

  private void CustomerBindingSource_PositionChanged(object sender, EventArgs e) { if(<yourCombobox>.SelectedIndex==-1) { CustomerBindingSource.SuspendBinding(); } else { CustomerBindingSource.ResumeBinding(); } } 
+1
source

What I use to "clear" the BindingSource is to simply set its DataSource as follows:

CustomerBindingSource.DataSource = typeof (Client);

Hope this helps.

EDIT:

For clarity, when you set the BindingSource.DataSource property as described, nothing prevents you from reassigning 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; ... 
0
source

Source: https://habr.com/ru/post/926371/


All Articles