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?

Recommended Answers

All 4 Replies

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.

Thanks!

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

Using a List<BindingSource> would leave no performance hit.

commented: Thanks! +5

Oooooh! Excellent idea! I like it! I don't know why I didn't think of that!

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.