开发者

how to implement undo operation in datagridview

开发者 https://www.devze.com 2022-12-09 16:23 出处:网络
I have created one application in c#.net.Using this application we can update datagridview,now i need to implement undo in it plz give me some ideas.

I have created one application in c#.net.Using this application we can update datagridview,now i need to implement undo in it plz give me some ideas.

 private void button29_Click(object sender, EventArgs e)
    {

           Datatable dt;
          dt.RejectChanges();
开发者_运维知识库

    }

using above code i can do undo before updating. but i need a undo feature as in word plz suggest me thanks in advance


You will have to create an undo stack. Populate this stack after each editing action on the data table with enough info to undo it. Then provide a mechanism to perform the actions in the stack in reverse manner.

say, you have two columns in your table, int c1 and varchar c2. Just to hint you, here are some sample classes.

struct ColData{
 int c1;
 string c2;
};

class Action {
 public abstract bool Undo(){}
 public abstract bool Redo(){}
}

class AddAction : Action{
 ColData data;
 public AddAction(ColData d){data = d;}
 public override bool Undo(){
     //remove the row
     //identify it with info from local field 'data'
 }
 public override bool Redo(){
     //add the row with info from local field 'data'
 }

}
//other actions like Delete, Update

And so on for all other actions. For editing actions you may need two sets of column values: old and new, so that you could repeat the change back and forth. Each time you have a change, you push a corresponding Action onto the action stack. When asked to undo, you pop it out of undo stack, call it's Undo method, and push it onto the redo stack. If you are asked to redo some action, you pop from redo stack, call Redo, push to undo stack. When you have another new action you should clear you redo stack and push the new action to the undo stack.


To get a more subtle undo, you can use the RejectChanges() at the DataRow level. I don't recall the DataTable keeping a sequence of edits (why would it) so you would have to setup your own list (lifo stack) of 'RowChanges'.

0

精彩评论

暂无评论...
验证码 换一张
取 消