Overview

A workbook contains multiple worksheets. 328

The screen component structure is shown in the following picture. 215

Control is Workbook

The ReoGrid control itself is a workbook instance.

var workbook = reoGridControl1;

Access worksheet

A workbook contains multiple worksheets. To access the currently active worksheet, use the CurrentWorksheet property.

var worksheet = reoGridControl.CurrentWorksheet;

To access a specific worksheet by name or index, use the Worksheets collection property:

var sheet1 = reoGridControl.Worksheets[0];
var sheet2 = reoGridControl.Worksheets["Sheet2"];

To change the current worksheet, set the CurrentWorksheet property to another worksheet instance.

var anotherWorksheet = reoGridControl.Worksheets["sheet2"];

if (anotherWorksheet != null)
{
  reoGridControl.CurrentWorksheet = anotherWorksheet;
}

::info The CurrentWorksheet property cannot be set to null. ::

Worksheet Management

Create worksheet

// create worksheet
var sheet = grid.CreateWorksheet();

// create worksheet with a specified name
var sheet = grid.CreateWorksheet('mysheet');

If no name is provided, ReoGrid will automatically find and use an available name such as ‘Sheet1’, ‘Sheet2’, etc. from the current workbook.

::info Like Excel, ReoGrid does not allow two worksheets in a single workbook to have the same name. ::

Adding/Inserting worksheet

// call method
grid.AddWorksheet(sheet);
grid.InsertWorksheet(1, sheet);

// use worksheet collection
grid.Worksheets.Add(sheet);
grid.Worksheets.Insert(1, sheet);

Like Excel, a workbook can contain multiple worksheets. Worksheets can be switched and displayed using the sheet tab control located at the bottom of the spreadsheet control. 217

To access the currently active worksheet, use the CurrentWorksheet property.

var sheet1 = reoGridControl.CurrentWorksheet;

Changing the active worksheet is accomplished by setting the CurrentWorksheet property.

reoGridControl.CurrentWorksheet = reogridControl.Worksheet[2];

Component object structure

/docs/images/231

Worksheet Management

Creating Worksheets

// Creating a worksheet
var sheet = grid.CreateWorksheet();

// Creating a worksheet with a specified name
var sheet = grid.CreateWorksheet("mysheet");

If no name is provided, or if the provided name is empty or null, ReoGrid automatically assigns an available name such as ‘Sheet1’, ‘Sheet2’, etc.

Adding/Inserting Worksheets

// Calling methods to add or insert worksheets
grid.AddWorksheet(sheet);
grid.InsertWorksheet(1, sheet);

// Using the worksheets collection to add or insert
grid.Worksheets.Add(sheet);
grid.Worksheets.Insert(1, sheet);

::info Within a workbook, two worksheets cannot share the same name. ::

Copying a Worksheet

// Copy the first worksheet and insert the copy at the second position.
// An optional name for the new sheet can be specified as an additional argument.
var sheet2 = grid.CopyWorksheet(0, 1);

Moving a Worksheet

// Move the first worksheet to the fourth position (indexing starts at 0)
grid.MoveWorksheet(0, 3);

Find worksheet index

Given the name of a worksheet, use the GetWorksheetIndex method to find its index in the workbook:

var index = grid.GetWorksheetIndex("sheet2");

Duplicate a worksheet

var sheet2 = grid.CopyWorksheet(0, 1);
var sheet3 = grid.CopyWorksheet("sheet2", 2);

Move worksheet

grid.MoveWorksheet(0, 3);

Get parent workbook

Use the Workbook property of a worksheet to get its parent workbook.

var workbook = grid.Workbook;

Events

ReoGrid provides a suite of events to track changes in worksheet management:

  • WorksheetCreated - Triggered when a new worksheet is created.
  • WorksheetInserted - Triggered when a worksheet is inserted into the workbook.
  • WorksheetRemoved - Triggered when a worksheet is removed from the workbook.
  • BeforeWorksheetNameChange - Triggered before the name of a worksheet is changed, allowing for custom validation or cancellation.
  • WorksheetNameChanged - Triggered after the name of a worksheet has been changed.

Resetting a Workbook

To return a workbook to its initial state, use the Reset method:

// Reset the workbook, clearing all content and restoring default settings
workbook.Reset();

Sheet tab control

Both the Windows Forms and WPF editions of ReoGrid provide a built-in sheet tab user interface control. Learn more about the sheet tab control.

Memory Workbook

ReoGrid also provides a memory workbook instance, which is a collection of worksheets without a GUI. See Memory workbook.

Was this article helpful?