Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:55:32+00:00 2026-06-17T13:55:32+00:00

I have a fairly standard requirement — I need to be able to open

  • 0

I have a fairly standard requirement — I need to be able to open a dialog where user can change values in data-bound fields, and then choose to click OK or Cancel, where clicking Cancel reverts the changes.

I’ve looked at IEditableCollectionView, IEditableObject and BindingGroups, but they all seem to be meant for editing a single item at a time. My program provides a collection of objects in a list, user selects an item from the list and edits it using SelectedItem-bound TextBoxes. Meaning that any number of items may be edited, including adding and removing them from the list, and all of those changes need to be reverted if he presses cancel.

At first I was simply making object backups through deep-copy (serialization) and restoring them on cancel, but now the objects must contain references to other, shared objects, making this approach problematic.

What’s the best way to approach such a scenario without manually copying objects and/or values back and forth?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:55:33+00:00Added an answer on June 17, 2026 at 1:55 pm

    After more thought on the matter, I have concluded that the best way, at least for small-scale implementation, is to write a “by value deep copy” method that copies values of objects fields and properties without replacing the objects themselves (so that any references to the edited objects remain intact even when data is restored).

    For this purpose I have written the following extension method:

    public static void CopyDataTo(this Object source, Object target) {
        // Recurse into lists
        if (source is IList) {
            var a = 0;
            foreach (var item in (IList)source) {
                if (a >= ((IList)target).Count) {
                    var type = item.GetType();
                    var assembly = Assembly.GetAssembly(type);
                    var newItem = assembly.CreateInstance(type.FullName);
                    ((IList)target).Add(newItem);
                }
                item.CopyDataTo(((IList)target)[a]);
                a++;
            }
            while (a < ((IList)target).Count) {
                ((IList)target).RemoveAt(a);
            }
        }
    
        // Copy over fields
        foreach (var field in source.GetType().GetFields())
            field.SetValue(target, field.GetValue(source));
        // Copy properties
        foreach (var property in source.GetType().GetProperties().Where(
            property => property.CanWrite && !property.GetMethod.GetParameters().Any()))
        {
            property.SetValue(target, property.GetValue(source));
        }
    }
    

    It’s no silver bullet: it only works on objects of the same type, list items have to have a parametrless constructor and there is no way to control recursion depth. In addition, I haven’t yet had a chance to test in any long-term or more complex scenarios, but so far it does what it should (copies values between objects) and can be used for a simple backup/restore scenario:

    var backup = new TypeOfVariableToEdit();
    data.CopyDataTo(backup);
    var clickedOK = RunDataEditor(data);
    if (!clickedOK)
        backup.CopyDataTo(data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Aptana have a file diff tool? Given it's a fairly standard dev requirement,
I have a fairly standard 'has_many :x, :through => :y' relationship with a user,
I have a fairly standard rails app and would like to be able to
I have a fairly standard Spring/Hibernate Java project where I need to set a
I have a fairly standard OLTP normalised database and I have realised that I
I have a fairly standard .Net MVC Controller method: public ActionResult Add(Customer cust) {
We have a fairly standard release procedure using Visual Source Safe for labeling a
So I have a fairly standard LINQ-to-Object setup. var query = expensiveSrc.Where(x=> x.HasFoo) .OrderBy(y
I'm fairly new to C++ standard library and have been using standard library lists
I have a fairly basic data.table in R, with 250k rows and 90 columns.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.