Workbook

Overview

A workbook contains multiple worksheet. 328

Screen component construction as following picture. 215

Control is Workbook

The ReoGrid control itself is a workbook instance.

var workbook = reoGridControl1;

Access worksheet

A workbook contains multiple worksheet, to access current activated worksheet, use CurrentWorksheet property.

var worksheet = reoGridControl.CurrentWorksheet;

To access a specified worksheet by its name or index, use Worksheets collection property:

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

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

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

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

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 the name is ignored, ReoGrid will find and use an available name such as 'Sheet1', 'Sheet2' ... from current workbook automatically.

Like Excel, ReoGrid doesn't 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 collection of worksheet
grid.Worksheets.Add(sheet);
grid.Worksheets.Insert(1, sheet);

Just 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 to another is accomplished by setting the CurrentWorksheet property.

reoGridControl.CurrentWorksheet = reogridControl.Worksheet[2];

Component object structure

/document/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");

In cases where no name is provided, or 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);

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

It is important to note that within a workbook, it is not permissible for two worksheets to 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

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

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 Workbook property of worksheet to get 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 ancellation.
  • WorksheetNameChanged - Triggered after the name of a worksheet has been changed.

Resetting a Workbook

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

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

Sheet tab control

Both windows form and WPF edition of ReoGrid provide a built-in sheet tab user interface control, learn more about sheet tab control.

Memory Workbook

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


Was the content of the page helpful?