简体   繁体   中英

How can I get two controls on separate forms bound to the same datasource to update each other?

I have a form with a lot of fields on it, most of which are optional; for reasons beyond my control, the required fields are scattered throughout the form rather than, say, all grouped together at the top by a big "REQUIRED" label.

I've been asked to make a separate form that can be launched from the main form that has only the required fields. The idea is that a user can bring up that "required fields" form, enter the necessary data which automatically shows up in the main form as they enter it, then close the "required fields" form and fill in any optional fields they want on the main form, and then finally save the form.

I've got the fields on the "required" form bound to the exact same data sources as their associated fields in the main form, and I know that's working properly because I can enter information only in the "required" form and then save, and the entered data gets saved. My problem is that while the data source behind the scenes gets updated, the corresponding field on the other form doesn't . I'd like to be able to have anything typed in a required field on either form instantly show up in the corresponding field on the other form, so that people understand it's properly storing their input regardless of which field they use.

I know how to manually refresh a field to reflect changes to its bound data, but doing it that way would entail a bunch of event handlers in both forms saying, "if field X changed, go change field X in the other form", which is both ludicrously repetitive and way too coupled. Is there a way I can set the controls to automatically refresh when their bound data source changes? The bound data source is a table in a System.Data.DataSet, so making my own data source that implements INotifyPropertyChanged seems like overkill; is there some other way to do this that I haven't noticed?

INotifyPropertyChanged is the mechanism for automatic refreshing in .NET, so that is what you need to implement if you want your form to automatically update in response to the bound data changing.

If you have a UI control bound to a data source, there are two ways for the UI control to get updated:

  1. UI control polls the data source every time period T
  2. Data source notifies the UI control when something changes

Polling is bad for performance reasons. INotifyPropertyChanged is the mechanism by which data sources notify UI controls of changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM