SharpCell.Grid

SpreadsheetView

public class SpreadsheetView : FrameworkElement

Namespace SharpCell.Grid.Wpf · package SharpCell.Grid.Wpf

Shows a SharpCell worksheet as Excel does: headers, gridlines, each cell's text as Excel formats it in the cell's style, the sheet's column widths, row heights, merged cells, frozen panes and gridlines. Cells are selected as in Excel: by clicking and dragging, by Shift with a click or the arrows, whole columns and rows from their headers or with Ctrl+Space and Shift+Space, the whole sheet from the corner or with Ctrl+A. Enter and Tab move inside a selection, the wheel scrolls and with Ctrl zooms, and Ctrl+C puts the selected cells on the clipboard as Excel pastes them. Columns and rows are resized by dragging the edges of their headers, and fitted by double-clicking them; the fill handle fills cells, and the headers have Excel's menu to insert, delete and hide them. Only the visible cells are read, so any sheet size works. The view reads cells when it paints; after changing cells or recalculating, call UIElement.InvalidateVisual. Sizes and frozen panes are taken when SpreadsheetView.Worksheet is set; after changing them, call SpreadsheetView.ReloadLayout.

Constructors

SpreadsheetView()

public SpreadsheetView()

Creates an empty view; set SpreadsheetView.Worksheet to show a sheet.

Properties

ActiveCell

public CellPosition? ActiveCell { get; set; }

The cell with the active-cell border; null shows none. It is the active cell of SpreadsheetView.Selection; setting it selects that cell alone.

EditText

public string? EditText { get; set; }

The text being typed into the active cell, which SpreadsheetView.CommitEdit enters; null when no cell is being edited. Setting it while editing changes the text, as a formula bar does.

Editor

public GridEditor? Editor { get; }

What changes the cells, with the history to undo and redo; one per workbook, created when SpreadsheetView.Worksheet is set. Null without a sheet.

IsEditing

public bool IsEditing { get; }

Whether a cell is being edited.

IsReadOnly

public bool IsReadOnly { get; set; }

Whether cells cannot be changed: no typing, Delete, pasting, undo or redo.

Offset

public Vector Offset { get; set; }

How far the cells are scrolled, in pixels from cell A1; kept inside the sheet when painting.

Painter

public GridPainter Painter { get; }

Geometry and colours: row heights, column widths, header sizes, theme.

Selection

public GridSelection? Selection { get; set; }

The selected cells, the active cell among them; null selects none. A new sheet starts from its active cell alone.

Viewport

public GridViewport Viewport { get; }

What the view shows now: its size and its offset, kept inside the sheet.

Worksheet

public Worksheet? Worksheet { get; set; }

The sheet shown; null shows an empty grid.

Zoom

public double Zoom { get; set; }

How large the cells are shown, 1 for Excel's 100%, from 0.1 to 4; Ctrl with the wheel changes it in steps of 10%. The cell at the top left stays there.

Methods

BeginEdit(string)

public bool BeginEdit(string? text = null)

Starts editing the active cell, as F2 does: with its formula or value as Excel shows it to edit, or with text. False when the view is read-only or has no sheet.

CancelEdit()

public void CancelEdit()

Ends editing without changing the cell, as Escape does.

ClearSelection()

public void ClearSelection()

Clears the content of the selected cells, as Delete does; formats and styles stay.

CommitEdit()

public bool CommitEdit()

Enters the text being typed into the active cell, as Enter does, and ends editing; true also when nothing is being edited. A formula that does not parse keeps the editor open, with the reason shown and the caret where the formula went wrong, and gives false.

Copy()

public void Copy()

Puts the selected cells on the clipboard as Excel pastes them: as text with tabs between cells, and as an HTML table with their styles. Ctrl+C and ApplicationCommands.Copy do it too.

Cut()

public void Cut()

Cuts the selected cells, as Ctrl+X and ApplicationCommands.Cut do in Excel: they are copied, and the next paste moves them, formulas and formats with them; until then they stay. Escape or another change forgets the cut.

DeleteCells(DeleteShift)

public void DeleteCells(DeleteShift shift)

Deletes the selected cells, moving the cells below or on the right into their place, as Excel's Delete dialog does.

Parameters

DeleteColumns()

public void DeleteColumns()

Deletes the selection's columns.

DeleteRows()

public void DeleteRows()

Deletes the selection's rows, as Ctrl+Minus does with rows selected.

FillDown()

public void FillDown()

Copies the selection's top row into the rows below it, formulas following, as Ctrl+D does; with one row selected, the row above it into it (see GridEditor.FillDown).

FillRight()

public void FillRight()

Copies the selection's left column into the columns right of it, as Ctrl+R does; see SpreadsheetView.FillDown.

HideColumns()

public void HideColumns()

Hides the selection's columns, as Ctrl+0 does.

HideRows()

public void HideRows()

Hides the selection's rows, as Ctrl+9 does.

InsertCells(InsertShift)

public void InsertCells(InsertShift shift)

Inserts empty cells in place of the selection, moving the cells beside it down or right, as Excel's Insert dialog does.

Parameters

InsertColumns()

public void InsertColumns()

Inserts as many columns as are selected before the selection's first column.

InsertRows()

public void InsertRows()

Inserts as many rows as are selected before the selection's first row, as Ctrl+Plus does with rows selected.

OpenDeleteDialog()

public void OpenDeleteDialog()

Opens Excel's Delete dialog for the selection: shift cells left or up, or delete entire rows or columns.

OpenInsertDialog()

public void OpenInsertDialog()

Opens Excel's Insert dialog for the selection: shift cells right or down, or insert entire rows or columns. Ctrl+Plus opens it when the selection is not whole rows or columns.

Paste()

public void Paste()

Pastes the text on the clipboard at the selection's top-left cell, as Ctrl+V and ApplicationCommands.Paste do: text copied from Excel goes in cell by cell, each read as typed. After SpreadsheetView.Cut, the cut cells move there instead, formulas and formats with them. The pasted cells are selected.

Redo()

public void Redo()

Redoes the last undone change and selects its cells, as Ctrl+Y does.

ReloadLayout()

public void ReloadLayout()

Takes the sheet's column widths, row heights, hidden rows and columns and frozen panes again.

Restyle(Func<CellStyle, CellStyle>)

public void Restyle(Func<CellStyle, CellStyle> change)

Changes the style of the selected cells: the function gets each cell's style and gives the new one.

SetBorders(BorderEdges, CellBorder)

public void SetBorders(BorderEdges edges, CellBorder? border)

Puts a border on edges of the selection, or takes it off with null, as Excel's Borders menu does; see GridEditor.SetBorders.

SetFill(CellColor?)

public void SetFill(CellColor? color)

Fills the selected cells with a colour, or takes their fill off with null, as Excel's Fill Color does.

SetFontColor(CellColor?)

public void SetFontColor(CellColor? color)

Gives the selected cells' text a colour, or the automatic colour with null, as Excel's Font Color does.

SetNumberFormat(string)

public void SetNumberFormat(string format)

Gives the selected cells a number format, as Excel's Number Format box does.

ShowNextSheet(int)

public bool ShowNextSheet(int direction = 1)

Shows the next sheet of the workbook, or with -1 the one before, as Ctrl+Page Down and Ctrl+Page Up do; false when there is none that way.

ToggleBold()

public void ToggleBold()

Makes the selected cells bold, or not bold when the active cell is, as Ctrl+B does.

ToggleItalic()

public void ToggleItalic()

Makes the selected cells italic, or not italic when the active cell is, as Ctrl+I does.

ToggleStrikethrough()

public void ToggleStrikethrough()

Strikes the selected cells through, or not when the active cell is, as Ctrl+5 does.

ToggleUnderline()

public void ToggleUnderline()

Underlines the selected cells, or takes the underline off when the active cell has one, as Ctrl+U does.

Undo()

public void Undo()

Undoes the last change to the cells and selects them, as Ctrl+Z does.

UnhideColumns()

public void UnhideColumns()

Shows the hidden columns among the selection's, as Ctrl+Shift+0 does.

UnhideRows()

public void UnhideRows()

Shows the hidden rows among the selection's, as Ctrl+Shift+9 does.