Worksheet

The worksheet view comprises three distinct sections:

  • Row Header View
  • Column Header View
  • Cells View

216

Management worksheet

Getting current active worksheet:

var worksheet = reoGridControl.CurrentWorksheet;

Changing current active worksheet

reoGridControl.CurrentWorksheet = anotherWorksheet;

It isn't able to set CurrentWorksheet as null, the control must keep at latest one worksheet as activated.

Getting worksheet by index

var worksheet = reoGridControl.Worksheets[1];

Getting worksheet index

var index = workbook.GetWorksheetIndex(mysheet);

Getting worksheet by name

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

Changing worksheet name

worksheet.Name = "newSheet";

Make sure the names from multiple worksheet not duplicate inside one 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 A1 cell:

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

Get cell data via worksheet index property:

var text = worksheet["A1"];

Method 2: Set by cells instance

Cells property of worksheet is used to get cells instance:

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

The cells instance provides not only the Data property, but also many stuff of cells, such as formula, style, border and etc. See cell.

Method 3: Set cells data by using action

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

Changing cells data by using action will get the ability to revoke the changes by using Undo method.

Accessing Cell Instances

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

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

Directly accessing the Cells property to retrieve a cell instance prompts ReoGrid to create a new instance if it does not already exist. To prevent unnecessary creation of cell instances, especially when performing checks on a cell's value or formula, it is recommended to use specific GetCellXXX methods, such as GetCellData. This approach avoids the overhead of creating numerous cell instances.

For further information, explore the Cell documentation.

Get cells instance without creating new instance

The GetCell method used to get cells instance without creating new instance:

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

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

Access rows and columns header

To access rows and columns header, use RowHeaders and ColumnHeaders property of worksheet:

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

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

For details see Row and Column.

Access styles

The styles of cells could 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 cells border, use SetRangeBorders method of 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 the powerful functionality on creating printable spreadsheet report.

224

For details see Paging and Print.

Change appearance and behavior

Hide row and column header

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

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

Result: 223

Learn more about worksheet settings, see Settings.

Change focus selection move direction

By default ReoGrid moves the focus selection horizontally when pressed Enter and Tab key. 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 following code:

worksheet.SelectionMode = ReoGridSelectionMode.None;

Allow/disallow changing rows and columns size

By default the row height and column width could be changed by dragging mouse on separator of headers, this behavior could be disabled by following code:

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

Learn more about worksheet settings, see Settings.


Was the content of the page helpful?