Worksheet

The view of worksheet contains three views:

  • 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;

Notice that cannot set null as value for CurrentWorksheet property, control should keep having an valid worksheet as current active worksheet.

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.

Create/Insert/Delete/Move/Copy worksheets

To create, insert, delete, move and copy worksheets inside workbook, see Workbook.

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.

Access cells instance

To access the cell instances, use Cells property of worksheet:

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

High Importance-20 The Cells property always return cell instance even no data and styles exist inside the cell. This is an safe and easy way to access cell instance, but it uses more memory when access to many cells.

Learn more about cells.

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 styles.

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;

For more about behaviors of worksheet, see Behavior.

Disable and hide selection

To disable and hide the selection, use following code:

worksheet.SelectionMode = ReoGridSelectionMode.None;

For more about behaviors of worksheet, see Behavior.

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.


Next: Cell

 

3 Responses to “Worksheet”

  1. Chris says:

    Hi, How do I find the value within the active/highlighted cell