Spread中类似Excel的三种删除方法: 撤销、重做

Spread Winform中响应delete按键实现数据删除,需要手写代码。 在实践的过程发现有3种办法可供选择:<ul> <li>响应KeyDown事件 </li> <li>自定义Action </li> <li>自定义UndoAction </li></ul><a href="http://www.grapecity.com.cn/image.ashx?picture=Spread_delete.gif"><img title="Spread_delete" style="display: inline" alt="Spread_delete" src="http://www.grapecity.com.cn/image.ashx?picture=Spread_delete.gif" width="600" height="470" /></a>

发布于 2013/12/04 00:00

SpreadJS

Spread Winform中响应delete按键实现数据删除,需要手写代码。 在实践的过程发现有3种办法可供选择:

  • 响应KeyDown事件
  • 自定义Action
  • 自定义UndoAction
    最终效果见截图:

Spread_delete比如我是选中A4:B5 的8个单元格,在按delete键,按照excel的话,直接就删除八个里的数据

 

响应KeyDown事件

判断当前按键为delete键,对selection内容ActiveSheet.ClearRange即可,代码如下所示:

  1: this.fpSpread1.KeyDown += new KeyEventHandler(spread_KeyDown);
  2: 
  3: private void spread_KeyDown(object source, KeyEventArgs e)
  4:         {
  5:             if (e.KeyCode == Keys.Delete)
  6:             {
  7:                 FpSpread spread = (FpSpread)source;
  8:                 int selectionCount = spread.ActiveSheet.SelectionCount;
  9:                 if (selectionCount > 0)
 10:                 {
 11:                     for (int i = 0; i < selectionCount; i++)
 12:                     {
 13:                         CellRange range = spread.ActiveSheet.GetSelection(i);
 14:                         spread.ActiveSheet.ClearRange(range.Row, range.Column, range.RowCount, range.ColumnCount, true);
 15:                     }
 16:                 }
 17:                 else
 18:                 {
 19:                     int activeRow = spread.ActiveSheet.ActiveRowIndex;
 20:                     int activeColumn = spread.ActiveSheet.ActiveColumnIndex;
 21:                     spread.ActiveSheet.ClearRange(activeRow, activeColumn, 1, 1, true);
 22:                 }
 23:                 e.Handled = true;
 24:             }
 25:         }
 26: 

 

自定义Action

前面有一篇博文已经针对专门写的,且有C#、VB的源码:

自定义Spread中快捷键行为

 

自定义UndoAction

当有了删除操作,就会有Undo的潜在需求,玩意误删除怎么办呢?  毕竟Spread的UI和Excel非常相似。实现一个Undo、Redo操作在Spread下也非常简单。

需要一个继承UndoAction的类,每次撤销、重做算一次完整的操作,会实例化一个UndoAction继承类。

源码如下所示:

  1:             this.fpSpread1.GetInputMap(InputMapMode.WhenFocused).Put(new Keystroke(Keys.Delete, Keys.None), FarPoint.Win.Spread.SpreadActions.ClearSelectedCells);
  2:             this.fpSpread1.GetActionMap().Put(SpreadActions.ClearSelectedCells, new ClearSelectedCellsUndoAction());
  3: 
  4: 
  5:     public class ClearSelectedCellsUndoAction : FarPoint.Win.Spread.UndoRedo.UndoAction
  6:     {
  7:         SpreadView spreadView = null;                         // SpreadView where action happens
  8:         FarPoint.Win.Spread.SheetView activeSheet = null;     // active SheetView in root workbook
  9:         FarPoint.Win.Spread.SheetView sheet = null;           // SheetView where action happens 
 10:         FarPoint.Win.Spread.Model.CellRange cellRange = null; // CellRange being cleared
 11:         DataObject clipData = null;                           // DataObject containing data from CellRange
 12: 
 13:         public override bool PerformUndoAction(object sender)
 14:         {
 15:             SpreadView rootWorkbook = sender as SpreadView;     // sender is always root SpreadView when called from UI
 16: 
 17:             if (rootWorkbook != null) // but sender might be null if other code calls this, so check!
 18:             {
 19:                 activeSheet = rootWorkbook.Sheets[rootWorkbook.ActiveSheetIndex]; // save active SheetView (to restore on undo)
 20:                 spreadView = rootWorkbook.GetActiveWorkbook();                    // save active SpreadView (to restore on undo)
 21:                 sheet = spreadView.GetSheetView();                                // get SheetView to operate on
 22:                 cellRange = sheet.GetSelection(sheet.SelectionCount - 1);         // get CellRange to operate on
 23:                 if (cellRange == null)  // GetSelection return null if there is no selection, just active cell
 24:                     cellRange = new CellRange(sheet.ActiveRowIndex, sheet.ActiveColumnIndex, 1, 1); // use active cell in this case
 25:             }
 26:             if (SaveUndoState()) // save data from range
 27:             { // then clear it
 28:                 sheet.ClearRange(cellRange.Row, cellRange.Column, cellRange.RowCount, cellRange.ColumnCount, false);
 29:                 return true;
 30:             }
 31:             return false; // something failed, return false to discard action from undo stack
 32:         }
 33: 
 34:         protected override bool SaveUndoState()
 35:         {
 36:             if (cellRange != null) // need CellRange set in PerformUndoAction (implied sheet is valid)
 37:                 clipData = sheet.GetClipDataObject(false, cellRange, ClipboardCopyOptions.All); // save data object for cell range
 38:             return clipData != null;
 39:         }
 40: 
 41:         public override bool Undo(object sender)
 42:         {
 43:             SpreadView rootWorkbook = sender as SpreadView;
 44:             if (rootWorkbook != null)
 45:             {
 46:                 rootWorkbook.ActiveSheetIndex = rootWorkbook.Sheets.IndexOf(activeSheet); // restore active sheet (in case user changed it)
 47:                 rootWorkbook.SetActiveWorkbook(spreadView); // restore active workbook in sheet (in case user changed it)
 48:                 sheet.ClearSelection(); // reset selection (clear, then add)
 49:                 sheet.AddSelection(cellRange.Row, cellRange.Column, cellRange.RowCount, cellRange.ColumnCount);
 50:                 sheet.ClipboardPaste(ClipboardPasteOptions.All, clipData); // paste data pack from data object
 51:                 return true;
 52:             }
 53:             return false;
 54:         }
 55:     }
 56: 

 

C#源码如下:

SpreadJS | 下载试用

纯前端表格控件SpreadJS,兼容 450 种以上的 Excel 公式,具备“高性能、跨平台、与 Excel 高度兼容”的产品特性,备受华为、苏宁易购、天弘基金等行业龙头企业的青睐,并被中国软件行业协会认定为“中国优秀软件产品”。SpreadJS 可为用户提供类 Excel 的功能,满足表格文档协同编辑、 数据填报、 类 Excel 报表设计等业务场景需求,极大的降低企业研发成本和项目交付风险。

如下资源列表,可以为您评估产品提供帮助:

相关产品
推荐相关案例
推荐相关资源
关注微信
葡萄城社区二维码

关注“葡萄城社区”

活字格低代码二维码

关注“活字格低代码”

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态