0

I have 7 different bindingsources on a form right now and I have a "cancel" button to allow a user to reject the changes they made and revert back to the original data in the dataset.

Instead of having my cancel button click event call something like:

private void buttonCancelChanges_Click(object sender, EventArgs e)
        {
            dS_DataSet.RejectChanges();
            BindingSource1.ResetBindings(true);
            BindingSource2.ResetBindings(true);
            BindingSource3.ResetBindings(true);
            BindingSource4.ResetBindings(true);
            BindingSource5.ResetBindings(true);
            BindingSource6.ResetBindings(true);
            BindingSource7.ResetBindings(true);

            ToggleEditMode();
        }

can I just loop through all of the existing binding sources on the form? I can't figure out how to do that if you can.

Any suggestions, or is this really the only way to do it?

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by zachattack05
0

Instead of using the designer for these binding sources, you can create a list of them programatically, then foreach the list to reset the bindings.

Alternatively, you could do something like:

foreach (BindingSource bs in this.components.Components.OfType<BindingSource>())
                bs.ResetBindings(true);

If you are worried about performance, I wouldn't use this too much. Especially if there are a lot of components in your form, since the filtering to find only the binding sources might take a while.

0

Thanks!

I think I'll stick with what I'm doing now instead, it's not worth a performance hit.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.