V4 V5

This page assumes unvell.ReoGrid.One is already installed (Installation).

A minimal app

using System.Windows.Forms;
using unvell.ReoGrid.Core;
using unvell.ReoGrid.Core.Style;
using unvell.ReoGrid.WinForms;

internal static class Program
{
	[STAThread]
	private static void Main()
	{
		// Ask for PerMonitorV2 on high-DPI displays. Instead of letting Windows scale a bitmap,
		// the control gets a physical-pixel client area and scales the drawing itself.
		Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);

		var grid = new ReoGridControl { Dock = DockStyle.Fill };
		BuildSheet(grid.ActiveWorksheet);

		var form = new Form { Text = "ReoGrid One", Width = 900, Height = 600 };
		form.Controls.Add(grid);
		Application.Run(form);
	}

	private static void BuildSheet(Worksheet sheet)
	{
		sheet.SetText(0, 0, "Item");
		sheet.SetText(0, 1, "Unit price");
		sheet.SetText(0, 2, "Qty");
		sheet.SetText(0, 3, "Subtotal");

		sheet.SetText(1, 0, "Notebook");
		sheet.SetNumber(1, 1, 320);
		sheet.SetNumber(1, 2, 12);
		sheet.SetFormula(1, 3, "B2*C2");     // no leading '='

		sheet.SetText(2, 0, "Ballpoint pen");
		sheet.SetNumber(2, 1, 150);
		sheet.SetNumber(2, 2, 40);
		sheet.SetFormula(2, 3, "B3*C3");

		sheet.SetText(4, 2, "Total");
		sheet.SetFormula(4, 3, "SUM(D2:D3)");

		// set the header row as a row style (do not walk the cells)
		sheet.SetRowStyle(0, new StyleRecord
		{
			Bold = true,
			BackgroundColor = 0xFFEFEFEF,      // ARGB
			TextAlign = HAlign.Center,
		});

		sheet.SetNumberFormat(new RangePosition(1, 1, 4, 3), "#,##0");
	}
}

new ReoGridControl() starts out with a workbook that already contains one sheet, Sheet1. ActiveWorksheet returns that sheet.

Key points

Formulas take no leading =

sheet.SetFormula(1, 3, "B2*C2");    // right
sheet.SetFormula(1, 3, "=B2*C2");   // wrong

SetFormula parses the formula, registers its dependencies, and recalculates that cell and every cell that depends on it. Call sheet.Recalculate() only when you want to recompute the whole sheet after a bulk load.

Colors are uint ARGB values

Colors are uint, not System.Drawing.Color. This keeps the core independent of System.Drawing; opaque colors start with 0xFF.

BackgroundColor = 0xFFEFEFEF,   // opaque light grey
Color = 0xFFCC0000,             // opaque red (text color)

Use row and column styles

To format a header row or an entire column, use SetRowStyle / SetColumnStyle instead of iterating over the cells. Styles are inherited in the order cell ◁ row ◁ column ◁ sheet, so setting the style once on the row is enough. Iterating over the cells of a sheet with a million rows makes the amount of work proportional to the sheet dimensions.

Styles are immutable records

StyleRecord is a record. To change only part of an existing style, use a with expression.

var current = sheet.GetCellStyle(1, 0) ?? StyleRecord.Default;
sheet.SetCellStyle(1, 0, current with { Bold = true });

Reading values

var text = sheet.GetDisplayText(4, 3);        // the display string after formatting
var raw = sheet.GetObjectValue(4, 3);         // double / string / bool / DateTime
var cell = sheet.GetValue(4, 3);              // CellValue (a struct; no boxing)

Writing through cell references

Cell(...) takes an A1-style address and returns a lightweight cursor. Calls can be chained.

sheet.Cell("B2").SetNumber(320);
sheet.Cell("D2").SetFormula("B2*C2");
sheet.Cell("A1").SetText("Item").SetStyle(new StyleRecord { Bold = true });

Main control operations

OperationMembers
Selection / active cellSelection, ActiveCell, SelectionChanged
Undo / redoUndo(), Redo(), CanUndo, CanRedo (Ctrl+Z / Ctrl+Y are wired up)
ZoomZoom (0.1–4.0), ZoomChanged. Ctrl+wheel also works
Freeze panesSetFreeze(rows, cols)
Formatting the selectionToggleSelectionBold(), SetSelectionBackColor(...), SetSelectionNumberFormat(...) and more
Inserting / deleting rows and columnsInsertRowsAtSelection(), DeleteSelectedRows() and others
Auto-fitting column widthsAutoFitSelectedColumns(), AutoFitSelectedRows()
Replacing the workbookLoadWorkbook(workbook), NewWorkbook(), LoadJson(json)
Was this article helpful?