如何实现datagridview的撤消操作如何实现、操作、datagridview

2023-09-07 00:54:27 作者:少年未老心已凉

我已经创建了C#一个应用.net.Using这个应用程序,我们可以更新datagridview的,现在我需要执行撤消它嘿给我一些想法。

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();


    }

使用上述$ C $词可以做更新前撤销。 但我需要一个撤消功能在字PLZ建议我 在此先感谢

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.

说,你有两列在表中,诠释C1​​和C2的varchar。只是暗示你,这里有一些示例类。

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

等了所有其他操作。对于编辑动作可能需要两套列值的:新与旧,这样你可以重复来回变化。 每次有一个变化,你把相应的动作到动作堆栈。当记者问到撤销,你弹出它撤消堆栈中,调用它的Undo方法,并将其推入重做堆栈。如果你被要求重做一些动作,你重做栈中弹出,呼叫重做,推动撤消堆栈。 如果您有其他新的动作,你应该清楚你重做栈和推新动作撤消堆栈。

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.