Append/Insert/Delete Operations

To manage rows and columns, the worksheet provides methods for appending, inserting, and removing them:

var sheet = grid.CurrentWorksheet;

// Append rows or columns at the end
sheet.AppendRows(int rowCount);
sheet.AppendCols(int colCount);

// Insert rows or columns at a specified position
sheet.InsertRows(int rowIndex, int rowCount);
sheet.InsertCols(int colIndex, int colCount);

// Delete rows or columns starting from a specified position
sheet.DeleteRows(int rowIndex, int rowCount);
sheet.DeleteCols(int colIndex, int colCount);

In these methods, the first parameter specifies the zero-based index at which the operation begins, and the second parameter indicates the number of rows or columns to be affected.

Alternatively, you can use actions for inserting or removing rows and columns, which support undo, redo, and repeat:

// Insert rows or columns
var insertRowAction = new unvell.ReoGrid.Actions.InsertRowsAction(rowIndex, rowCount);
var insertColAction = new unvell.ReoGrid.Actions.InsertColumnsAction(colIndex, colCount);

// Remove rows or columns
var removeRowAction = new unvell.ReoGrid.Actions.RemoveRowsAction(rowIndex, rowCount);
var removeColAction = new unvell.ReoGrid.Actions.RemoveColumnsAction(colIndex, colCount);

// Execute actions
sheet.DoAction(insertRowAction);
sheet.DoAction(insertColAction);
sheet.DoAction(removeRowAction);
sheet.DoAction(removeColAction);

Setting Row and Column Counts

To modify the number of rows and columns in a worksheet, the following methods are available:

// To resize both rows and columns
sheet.Resize(int rowCount, int colCount);

// To adjust the number of rows or columns individually
sheet.SetRows(6);  // Sets the number of rows
sheet.SetCols(5);  // Sets the number of columns

23

Predefined Values

The table below shows the initial, minimum, and maximum values for various worksheet parameters, along with their data types:

InitialMinimumMaximumData type
Number of Rows20011,048,576Paging-indexed two-dimensional array
Number of Columns100132,768Paging-indexed two-dimensional array
Row Height (pixels)20065,535ushort
Column Width (pixels)70065,535ushort

Adjusting Height and Width

To modify the height of rows or the width of columns, use the following methods:

// To set the height of all rows
sheet.SetRowsHeight(0, grid.RowCount, 10);

// To set the width of all columns
sheet.SetColsWidth(0, grid.ColCount, 10);

38

Alternatively, to adjust header sizes with undo support, use the following actions:

// To change the height of a range of rows
sheet.DoAction(new SetRowsHeightAction(0, 10, 30));

// To change the width of a range of columns
sheet.DoAction(new SetColumnsWidthAction(0, 10, 30));

::info The SetRowsHeightAction and SetColumnsWidthAction constructors require specifying the starting index, the number of rows or columns to affect, and the new height or width in pixels. These actions are part of ReoGrid’s undo/redo framework, allowing for more flexible document editing. ::

39

Auto row height

ReoGrid automatically adjusts the row height when a cell value changes through editing: 22

Disable auto adjusting

This behavior can be disabled for individual rows:

var rowHeader = sheet.RowHeaders[2];
rowHeader.IsAutoHeight = false;

Auto fit row height and column width

When you double-click on the separator between headers, ReoGrid adjusts the row height or column width to fit the largest cell.

Before: 218

After: 219

Code to do this programmatically:

sheet["A2"] = "This is a long text";
sheet.AutoFitColumnWidth(0, false);

The second argument specifies whether to use an action to perform this operation. Using an action enables undo via the Undo method or Ctrl+Z.

Hide & Unhide

Like Excel, ReoGrid supports hiding rows or columns. Call the following methods to hide or show rows or columns:

sheet.HideRows(int start, int count);
sheet.ShowRows(int start, int count);
sheet.HideColumns(int start, int count);
sheet.ShowColumns(int start, int count);

A hidden row or column is displayed as a single line. 96

Notes:

  • Both HideRows and HideColumns will automatically collapse any outlines whose rows or columns are being hidden.
  • Both ShowRows and ShowColumns will automatically expand any outlines whose rows or columns are being shown.

Example: 58 Hide rows grouped by an outline: 59 The outline will be collapsed automatically. 60

For more details, see Group & Outline.

Check for hidden headers and cells

Check whether a row or column is hidden:

bool Worksheet.IsHiddenRow(int row);
bool Worksheet.IsHiddenColumn(int col);

Check whether a cell is on a hidden row or column:

bool Worksheet.IsHiddenCell(int row, int col);
bool Worksheet.IsHiddenCell(string addressOrName);
bool Worksheet.IsHiddenCell(ReoGridCell cellInstance);

// or by calling from a cell instance
var cell = sheet.Cells["H8"];
bool hidden = cell.IsHidden;

Get the instances of header

// get worksheet instance from grid control
var sheet = grid.CurrentWorksheet;

// get header instances
var colHeader = sheet.ColumnHeaders[2];
var rowHeader = sheet.RowHeaders[3];

Change column header text

// set text of column header
colHeader.Text = "Product";

Custom Header

Change row header text

sheet.RowHeaders[1].Text = "Header";

117

Change row header width

You can change the width of the row header to display more text:

sheet.RowHeaderWidth = 100;
sheet.RowHeaders[1].Text = "Row Header";

116

Hide row via header instance

// hide 4th row (zero-based)
rowHeader.IsHidden = true;

98

Change width or height of header

sheet.RowHeaders[3].HeightInPixel = 30;   // height in pixel

Performance difference between instance call and Control API

Some operations can be performed via either a header instance call or a control API call, but header instance calls may have a performance impact. For the example below, it is strongly recommended to use Method 2.

Method 1: Call header instance to change height of rows

sheet.RowHeaders[2].HeightInPixel = 30;
sheet.RowHeaders[3].HeightInPixel = 30;
sheet.RowHeaders[4].HeightInPixel = 30;

Method 2: Call control API to change height of rows

sheet.SetRowsHeight(2, 3, 30);  // from index 2, rows is 3

Method 2 provides a significant performance advantage over Method 1.

Column Cells Type

You can specify a default cell type for an entire column.

// ColumnHeaders property returns an instance of column header
// it accepts a numeric index or an address string to locate a column
sheet.ColumnHeaders["A"].DefaultCellBody = typeof(unvell.ReoGrid.CellTypes.CheckBoxCell);

// set horizontal alignment for all cells in this column to center
sheet.ColumnHeaders["A"].Style.HorizontalAlign = ReoGridHorAlign.Center;

// give the check box a small padding (2 pixels)
sheet.ColumnHeaders["A"].Style.Padding = new System.Windows.Forms.Padding(2);

Setting the cell type alone will not cause any cells to be displayed; the cell body is displayed only when cell data is filled in. 110

sheet["A1:A5"] = new object[] { false, true, false, false, true };

The checkbox cell accepts a bool value as data; the grid is displayed as below: 111

Was this article helpful?