Ankit
Ankit

Reputation: 33

Undo redo in window form and gridcontrol

I have a window form that has multiple control and grid-view control and I want to add undo redo functionality for this form for 20 levels,please help.

dtStates = new DataTable();
            DataColumn dcIndex = new DataColumn("ID", typeof(int));
            dcIndex.AutoIncrement = true;
            dcIndex.AutoIncrementSeed = 1;
            dcIndex.AutoIncrementStep = 1;
            dtStates.Columns.Add(dcIndex);

            dtStates.Columns.Add("Control", typeof(Object));
            dtStates.Columns.Add("Type", typeof(Object));
            dtStates.Columns.Add("Value", typeof(String));
            dtStates.Columns.Add("ControlID", typeof(String));

This is datatable where I record all the action of the form.But in case of gridview ,i am confuse how to record and maintain changes.

Upvotes: 0

Views: 763

Answers (2)

Sebi
Sebi

Reputation: 3979

Your GridView just show data from a DataTable or better from List. You need to save this data. In fact your datasource. To save your objects you will need to do a deep copy (How do you do a deep copy of an object in .NET (C# specifically)?) of it. Then you just need a list to hold the different versions. If you using the memento design-pattern and generics you can build a general class for undo/redo which you can use in other programs or components too.

Maybe my example can help:

[Serializable()]
public class clsSchnappschuss<T>
{
    private MemoryStream mvArbeitspeicherZugriff;
    private BinaryFormatter mvFormatierer;

    public clsSchnappschuss()
    {
        if (Attribute.GetCustomAttribute(typeof(T), typeof(SerializableAttribute)) == null)
        {
            Trace.WriteLine(string.Concat(typeof(T).FullName, 
                                          " ist nicht serialisiebar!"));
            throw new InvalidOperationException(string.Concat(string.Format("{0} ist nicht serialisierbar.",
                                                                            typeof(T).FullName),
                                                                            " Die Klasse muss das Attribut ",
                                                                            "Serializable einbinden ",
                                                                            "[Serializable()] ",
                                                                            "um clsSchnappschuss verwenden zu ",
                                                                            "können."));
        }

        mvFormatierer = new BinaryFormatter();
    }

    public clsSchnappschuss<T> BxSpeichern(T obj)
    {
        mvArbeitspeicherZugriff = new MemoryStream();
        mvFormatierer.Serialize(mvArbeitspeicherZugriff, obj);
        return this;
    }

    public T BxWiederherstellen()
    {
        mvArbeitspeicherZugriff.Seek(0, SeekOrigin.Begin);
        mvFormatierer.Binder = new clsCustomBinder();
        T obj = (T)mvFormatierer.Deserialize(mvArbeitspeicherZugriff);
        mvArbeitspeicherZugriff.Close();
        return obj;
    }
}

In this class the data will be stored:

public class clsAufbewahrer<T>
{
    private List<clsSchnappschuss<T>> Liste;

    public clsAufbewahrer()
    {
        Liste = new List<clsSchnappschuss<T>>(10);
    }

    public clsAufbewahrer(int listenKapazität)
    {
        Liste = new List<clsSchnappschuss<T>>(listenKapazität);
    }

    public List<clsSchnappschuss<T>> Schnappschuesse 
    { 
        get; 
        set; 
    }
}

Upvotes: 1

Kai Hartmann
Kai Hartmann

Reputation: 3154

Create a list for each property you want to undo/redo for each of your controls. Save each change of such an property to that list. Every time your list exceeds 20 elements, remove the first entry. Now you should be able to find each value in order for each of your controls in the appropriate list.

Upvotes: 0

Related Questions