The worksheet view comprises three distinct sections:

  • Row Header View
  • Column Header View
  • Cells View

216

Managing Worksheets

Get the current active worksheet:

var worksheet = reoGridControl.CurrentWorksheet;

Changing the current active worksheet

reoGridControl.CurrentWorksheet = anotherWorksheet;

::info CurrentWorksheet cannot be set to null — the control must always keep at least one worksheet active. ::

Getting a worksheet by index

var worksheet = reoGridControl.Worksheets[1];

Getting a worksheet’s index

var index = workbook.GetWorksheetIndex(mysheet);

Getting a worksheet by name

var worksheet = reoGridControl.Worksheet["sheet2"];

Changing a worksheet’s name

worksheet.Name = "newSheet";

Worksheet names must be unique within the same workbook.

Creating/Inserting/Deleting/Moving/Copying worksheets

To create, insert, delete, move, or copy a worksheet, see Worksheet Management.

Access cells data

There are 3 methods to access cell data:

Method 1: Using worksheet index property

Display “hello world” in cell A1:

worksheet["A1"] = "hello world";

Get cell data via the worksheet index property:

var text = worksheet["A1"];

Method 2: Set by cells instance

The Cells property of the worksheet is used to get a cell instance:

var cell = worksheet.Cells["A1"];
cell.Data = "hello world";

The cell instance provides not only the Data property, but also many other properties such as formula, style, border, and more. See Cell.

Method 3: Set cells data using an action

var action = new SetCellDataAction("A1", "hello world");
reoGridControl.CurrentWorksheet.DoAction(action);

Changing cell data using an action enables the ability to undo the change with the Undo method.

Accessing Cell Instances

To interact with cell instances, use the Cells property of the worksheet:

var cell = worksheet.Cells["A1"];
cell.Data = "hello world";

::info Accessing the Cells property to retrieve a cell instance prompts ReoGrid to create a new instance if one does not already exist. To avoid unnecessary creation of cell instances — especially when only checking a cell’s value or formula — it is recommended to use specific GetCellXXX methods such as GetCellData. This avoids the overhead of creating large numbers of cell instances. ::

For further information, see the Cell documentation.

Get a cell instance without creating a new one

The GetCell method retrieves an existing cell instance without creating a new one:

var cell = worksheet.GetCell("A1");

if (cell != null) {
  cell.Data = "hello world";
}

Access rows and columns header

To access row and column headers, use the RowHeaders and ColumnHeaders properties of the worksheet:

var rowHeader = worksheet.RowHeaders[0];  // index: zero-based number of row
var colHeader = worksheet.ColHeaders[0];  // index: zero-based number of column

// a column header can also be located using an address code
var colHeader = worksheet.ColHeaders["B"];

For details see Row and Column.

Access styles

Cell styles can be accessed by specifying a range:

worksheet.SetRangeStyle("A1:C3", new WorksheetRangeStyle
{
  Flag = PlainStyleFlag.TextColor,
  TextColor = Color.Green,
});

Result: 221

For details see Cell Style.

Access borders

To change cell borders, use the SetRangeBorders method of the worksheet.

worksheet.SetRangeBorders("A1:C3", BorderPositions.InsideHorizontal,
  new unvell.ReoGrid.BorderStyle
  {
    Style = BorderLineStyle.Dashed,
    Color = Color.Pink,
  });

Result:

222

For details see Border.

Setup print and preview

ReoGrid provides powerful functionality for creating printable spreadsheet reports.

224

For details see Paging and Print.

Change appearance and behavior

Hide row and column header

To hide the row and column headers, set the worksheet settings:

worksheet.SetSettings(WorksheetSettings.View_ShowRowHeader, false);
worksheet.SetSettings(WorksheetSettings.View_ShowColumnHeader, false);

Result: 223

For more about worksheet settings, see Settings.

Change focus selection move direction

By default, ReoGrid moves the focus selection horizontally when the Enter or Tab key is pressed. The following code changes the direction from horizontal to vertical:

worksheet.SelectionForwardDirection = SelectionForwardDirection.Down;

Disable and hide selection

To disable and hide the selection, use the following code:

worksheet.SelectionMode = ReoGridSelectionMode.None;

Allow/disallow changing rows and columns size

By default, row height and column width can be changed by dragging the separator between headers. This behavior can be disabled with the following code:

worksheet.SetSettings(WorksheetSettings.Edit_AllowAdjustRowHeight, false);
worksheet.SetSettings(WorksheetSettings.Edit_AllowAdjustColumnWidth, false);

For more about worksheet settings, see Settings.

Was this article helpful?