Interactive Features in Windows Forms Spreadsheet

23 Jul 20267 minutes to read

This section covers the interactive operations in the Spreadsheet.

Clipboard Operations

Spreadsheet supports all clipboard operations, including all format settings when content is copied within a workbook.

You can use the following shortcut keys for clipboard operations, as in Excel.

Operations Shortcut Keys
Cut Ctrl + X
Copy Ctrl + C
Paste Ctrl + V

The following is a list of paste options available during a paste operation:

Options Description
Paste Paste with all format options from the source range
Formula Paste the formulas only
Keep Source Formatting Maintains the source range’s formatting
Value Paste the values only
Format Paste only the formats without pasting the values
Value & Source Formatting Maintains the source range’s original format and paste only values

NOTE

When the content is copied from external source, Spreadsheet does not support the format settings (paste options).

For Cut Operation,

//To perform the cut operation on the selected range
var range = spreadsheet.ActiveGrid.SelectedRanges.ActiveRange;
spreadsheet.ActiveGrid.CopyPaste.Copy(range, true);

//To perform the cut operation
spreadsheet.ActiveGrid.CopyPaste.Cut();

For Copy Operation,

//To perform the copy operation on the selected range
var range = spreadsheet.ActiveGrid.SelectedRanges.ActiveRange;
spreadsheet.ActiveGrid.CopyPaste.Copy(range, false);

//To perform the copy operation on the current selection
spreadsheet.ActiveGrid.CopyPaste.Copy();

For Paste Operation,

//To perform the paste operation on the current selection
spreadsheet.ActiveGrid.CopyPaste.Paste();

//To perform the paste operation with a range and paste options
var copyPaste = spreadsheet.ActiveGrid.CopyPaste as SpreadsheetCopyPaste;
copyPaste.Paste(range);
copyPaste.Paste(range, PasteOptions.Paste);

TIPS

Users can also set their default PasteOptions while pasting in Spreadsheet by using the DefaultPasteOption property.

Undo or Redo

Spreadsheet provides support for the Undo/Redo functionality like Microsoft Excel.

The following shortcut keys are used for Undo/Redo operations:

Operations Shortcut Keys
Undo Ctrl + Z
Redo Ctrl + Y

Spreadsheet has a History Manager class that supports the implementation of undo/redo operations.

By default, Undo/Redo operations in Spreadsheet are enabled. To disable the undo/redo operations, set the Enabled property of the History Manager to false.

spreadsheet.HistoryManager.Enabled = false;

To programmatically invoke the Undo/Redo operations:

spreadsheet.HistoryManager.Enabled = true;
spreadsheet.HistoryManager.Undo();
spreadsheet.HistoryManager.Redo();

Context menu

The Context menu in Spreadsheet is customizable and can be used for various functionalities.

TabItem Context menu

The TabItem Context menu opens when the user right-clicks on the sheet tab and contains menus related to worksheet operations.

By default, the TabItem context menu is enabled in Spreadsheet. To disable the TabItem Context menu, set the AllowTabItemContextMenu property to false.

spreadsheet.AllowTabItemContextMenu = false;

The default TabItem context menu has options like Insert, Delete, Hide/Unhide, and Protect sheet. You can also customize the TabItem Context menu by setting the IsCustomTabItemContextMenuEnabled property to true and adding your customized menu items.

spreadsheet.IsCustomTabItemContextMenuEnabled = true;
spreadsheet.TabItemContextMenu = CustomTabItemContextMenu();

//Custom TabItem ContextMenu

public ContextMenu CustomTabItemContextMenu()
{
    var contextMenu = new ContextMenuStrip();
    contextMenu.BackColor = Color.White;
    contextMenu.RenderMode = ToolStripRenderMode.System;
    var insertRowIcon = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\insertRow.png", UriKind.Relative)) };
    var insertRow = new ToolStripMenuItem() { BackColor = Color.White, Text = "InsertRow" };
    insertRow.Image = insertRowIcon;
    insertRow.Click += insertRow_Click;
    var deleteRowIcon = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\deleteRow.png", UriKind.Relative)) };
    var deleteRow = new ToolStripMenuItem() { BackColor = Color.White, Text = "DeleteRow" };
    deleteRow.Image = deleteRowIcon;
    deleteRow.Click += deleteRow_Click;
    contextMenu.Items.Add(insertRow);
    contextMenu.Items.Add(deleteRow);
    return contextMenu;
}

Cell Context menu

The Cell Context menu opens when the user right-clicks on a worksheet cell or a selection of cells in Spreadsheet.

By default, the Cell Context menu is enabled in Spreadsheet. To disable the Cell Context menu, set the AllowCellContextMenu property to false.

spreadsheet.AllowCellContextMenu = false;

Users can also customize the Cell Context menu of Spreadsheet by using the CellContextMenuOpening event of SpreadsheetGrid.

Adding the customized menu items in the CellContextMenuOpening event:

spreadsheet.ActiveGrid.CellContextMenuOpening += ActiveGrid_CellContextMenuOpening;

void ActiveGrid_CellContextMenuOpening(object sender, CellContextMenuOpeningEventArgs e)
{
    //Adding Customized Menu item
    var PasteSpecial = new ToolStripMenuItem(){ BackColor = Color.White, Name = "PasteSpecial"};
    PasteSpecial.Text = "PasteSpecial";
    Image paste = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\paste.png", UriKind.Relative)) };
    PasteSpecial.Image = paste;
    PasteSpecial.Click += PasteSpecial_Click;
    spreadsheet.ActiveGrid.CellContextMenu.Items.Add(PasteSpecial);
       
    //Remove the existing Context Menu item
    spreadsheet.ActiveGrid.CellContextMenu.Items.RemoveAt(2);
}

Cell Comments

Spreadsheet provides support for cell comments, like in Excel, to give the reader additional context for the data it contains. You can set the comment height and color for the particular comments at runtime by invoking the CellCommentOpening event of SpreadsheetGrid.

To enable comments in Spreadsheet, set the ShowComment property of SpreadsheetGrid to true.

spreadsheet.ActiveGrid.ShowComment = true;

To set the comment for a particular cell at run time:

spreadsheet.ActiveSheet.Range["E5"].AddComment().Text = "Sample Comment";
spreadsheet.ActiveGrid.InvalidateCell(5, 5);