实现撤销回撤功能datagridview(undo)(C#源码)

上传者: likaikk | 上传时间: 2024-11-23 10:58:55 | 文件大小: 151KB | 文件类型: RAR
在.NET框架中,`DataGridView`控件是用于展示表格数据的常用组件,广泛应用于Windows Forms应用程序。本篇文章将深入探讨如何在C#中为`DataGridView`实现撤销(Undo)和回撤(Redo)功能,这是一项对于用户交互非常重要的功能,尤其是在允许用户编辑表格数据的应用中。 撤销/回撤功能的核心思想是记录用户操作的历史,以便在需要时恢复到之前的状态。在C#中,我们可以使用Memento设计模式来实现这一功能。Memento模式通过保存和恢复对象的内部状态来实现对撤销/回撤的支持。 1. **创建Memento类**: 为`DataGridView`创建一个Memento类,该类存储`DataGridView`在特定时间点的行、列和单元格的数据。包括行的数量、行的索引、每行的单元格数据等。例如: ```csharp public class DataGridViewMemento { private List RowsSnapshot; private List ColumnsSnapshot; // 构造函数用于初始化快照 public DataGridViewMemento(DataGridView dataGridView) { RowsSnapshot = new List(dataGridView.Rows.Cast()); ColumnsSnapshot = new List(dataGridView.Columns.Cast()); } // 提供访问快照的方法 public List Rows { get { return RowsSnapshot; } } public List Columns { get { return ColumnsSnapshot; } } } ``` 2. **实现Undo/Redo栈**: 在你的主程序中,你需要两个栈,一个用于存储撤销操作(UndoStack),另一个用于存储回撤操作(RedoStack)。每次用户进行修改时,都将当前`DataGridView`的状态推送到UndoStack,并清空RedoStack。 ```csharp Stack UndoStack = new Stack(); Stack RedoStack = new Stack(); ``` 3. **监听事件**: 监听`DataGridView`的`CellValueChanged`或`UserDeletingRow`事件,当这些事件触发时,创建一个新的Memento实例并将其推送到UndoStack。 4. **实现Undo操作**: 当用户点击“撤销”按钮时,检查UndoStack是否为空,如果不为空,则弹出顶部的Memento,将`DataGridView`恢复到之前的状态,并将这个Memento推送到RedoStack。 5. **实现Redo操作**: 同理,当用户点击“回撤”按钮时,检查RedoStack是否为空,如果不为空,则弹出顶部的Memento,将`DataGridView`恢复到那个状态,并将这个Memento推送到UndoStack。 6. **注意事项**: - 考虑到性能,不要在每次单元格更改时都创建Memento,而是可以设置一个阈值,例如每5次更改才保存一次状态。 - 处理多线程情况时,确保对UndoStack和RedoStack的访问是线程安全的,可能需要使用`lock`语句或使用`ConcurrentStack`类。 - 考虑到内存占用,可能需要限制UndoStack和RedoStack的大小,超出限制时,丢弃较早的操作记录。 通过以上步骤,你可以为`DataGridView`实现撤销和回撤功能。记住,良好的用户交互体验是软件成功的关键,撤销/回撤功能能够极大地提高用户在处理数据时的满意度和效率。在实际项目中,你可能还需要根据具体需求对这个功能进行扩展,例如处理排序、过滤和分页等操作的撤销/回撤。

文件下载

资源详情

[{"title":"( 41 个子文件 151KB ) 实现撤销回撤功能datagridview(undo)(C#源码)","children":[{"title":"undo撤销","children":[{"title":"undo.suo <span style='color:#111;'> 36.00KB </span>","children":null,"spread":false},{"title":"undo","children":[{"title":"Form3.Designer.cs <span style='color:#111;'> 5.04KB </span>","children":null,"spread":false},{"title":"Form1.Designer.cs <span style='color:#111;'> 3.75KB </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 485B </span>","children":null,"spread":false},{"title":"Form2.Designer.cs <span style='color:#111;'> 3.59KB </span>","children":null,"spread":false},{"title":"Form3.cs <span style='color:#111;'> 1.80KB </span>","children":null,"spread":false},{"title":"Form2.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"x86","children":[{"title":"Debug","children":[{"title":"undo.csproj.FileListAbsolute.txt <span style='color:#111;'> 1.58KB </span>","children":null,"spread":false},{"title":"undo.pdb <span style='color:#111;'> 53.50KB </span>","children":null,"spread":false},{"title":"undo.Form3.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"undo.Form1.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 6.37KB </span>","children":null,"spread":false},{"title":"ResolveAssemblyReference.cache <span style='color:#111;'> 8.93KB </span>","children":null,"spread":false},{"title":"undo.Properties.Resources.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"GenerateResource.read.1.tlog <span style='color:#111;'> 758B </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"undo.Form2.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"undo.exe <span style='color:#111;'> 17.50KB </span>","children":null,"spread":false},{"title":"GenerateResource.write.1.tlog <span style='color:#111;'> 2.59KB </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferences.cache <span style='color:#111;'> 24.03KB </span>","children":null,"spread":false}],"spread":false}],"spread":true}],"spread":true},{"title":"bin","children":[{"title":"Release","children":null,"spread":false},{"title":"Debug","children":[{"title":"undo.pdb <span style='color:#111;'> 53.50KB </span>","children":null,"spread":false},{"title":"undo.vshost.exe <span style='color:#111;'> 11.33KB </span>","children":null,"spread":false},{"title":"ZCControl.dll <span style='color:#111;'> 92.00KB </span>","children":null,"spread":false},{"title":"undo.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"undo.exe <span style='color:#111;'> 17.50KB </span>","children":null,"spread":false},{"title":"ZCControl.pdb <span style='color:#111;'> 191.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Form1.cs <span style='color:#111;'> 2.12KB </span>","children":null,"spread":false},{"title":"MyButton.cs <span style='color:#111;'> 646B </span>","children":null,"spread":false},{"title":"Form2.cs <span style='color:#111;'> 1.23KB </span>","children":null,"spread":false},{"title":"Form1.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Canvas.cs <span style='color:#111;'> 285B </span>","children":null,"spread":false},{"title":"Form3.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Class1.cs <span style='color:#111;'> 6.63KB </span>","children":null,"spread":false},{"title":"MyDataGridView.cs <span style='color:#111;'> 8.19KB </span>","children":null,"spread":false},{"title":"undo.csproj <span style='color:#111;'> 4.64KB </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.31KB </span>","children":null,"spread":false},{"title":"Settings.Designer.cs <span style='color:#111;'> 1.06KB </span>","children":null,"spread":false},{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false},{"title":"Settings.settings <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"Resources.Designer.cs <span style='color:#111;'> 2.79KB </span>","children":null,"spread":false}],"spread":false},{"title":"IUndoRedo.cs <span style='color:#111;'> 287B </span>","children":null,"spread":false}],"spread":false},{"title":"undo.sln <span style='color:#111;'> 854B </span>","children":null,"spread":false}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明