Table Operations

<< Click to display table of contents >>

Table Operations

Operations on Selection

Inserting rows and columns:

InsertColsLeft,

InsertColsRight,

InsertRowsAbove,

InsertRowsBelow.

Deleting rows and columns:

DeleteSelectedCols,

DeleteSelectedRows,

Cell merging and unmerging:

MergeSelectedCells (see also CanMergeSelectedCells),

UnmergeSelectedCells,

SplitSelectedCellsHorizontally,

SplitSelectedCellsVertically

Other Operations

Inserting rows and columns:

InsertCols,

InsertRows.

Deleting rows and columns:

DeleteCols,

DeleteRows.

Cell merging and unmerging:

MergeCells (also CanMergeCells)

UnmergeCells,

DeleteEmptyRows, DeleteEmptyCols (use after cell merging).

Moving rows

MoveRows.

Assigning Table Properties (As Editing Operations)

Methods for assigning table properties:

SetTableVisibleBorders.

Methods for assigning row properties:

SetRowVAlign, SetRowKeepTogether, SetRowPageBreakBefore.

Methods for assigning cell properties:

SetCellColor, SetCellBorderColor, SetCellBorderLightColor;

SetCellVAlign;

SetCellRotation;

SetCellTag;

SetCellBestWidth, SetCellBestHeight;

SetCellBackgroundImage, SetCellBackgroundImageFileName, SetCellBackgroundStyle;

SetCellHint;

SetCellVisibleBorders;

SetCellOpacity;

SetCellOptions.

Operations in Viewer and Editor

If the operations above are performed before inserting the table, no addition actions are required.

If these operations are performed on table inserted in TRichView, you need to call Format method after to update the document view.

If these operations are performed on table inserted in TRichViewEdit, a special sequence of steps is required:

1.checking RichViewEdit.CanChange (especially important for data-aware versions of components);

2.obtaining table object and its position in editor;
(position is defined as item index + editor where this table is inserted (can be root editor or cell inplace-editor, referred below as rve)

3.calling rve.BeginItemModify for table

4.performing operations

5.calling rve.EndItemModify

6.calling rve.Change

The example is below.

You can group several actions so that they will be undone/redone as whole, using SetUndoGroupMode

In order to perform operation(s) on table, you need to get table object.You can do it using method

function TRichView.GetItem(ItemNo: Integer): TCustomRVItemInfo;

TCustomRVItemInfo is an ancestor class for all items of RichView, including table (TRVTableItemInfo). This method can be used for item of any type, so you need to check if it is a table (using "is" operator, or checking RichView.GetItemStyle(ItemNo)=rvsTable)

But usually, when you need to perform operations on the item at the position of caret in editor, you do not know if the current item is in "root" editor, or inside cell, or inside cell of table inside other cell, and so on.

In any case, you can get item at position of caret with method

function TRichViewEdit.GetCurrentItem: TCustomRVItemInfo;

But even if the caret is inside table, the current item will be not a table, but some other item in cell-inplace editor!

The problem can be solved with the method

function TRichViewEdit.GetCurrentItemEx(

  RequiredClass: TCustomRVItemInfoClass;

  out ItemRichViewEdit: TCustomRichViewEdit;

  out Item: TCustomRVItemInfo): Boolean;

In this method,

RequiredClass set it to TRVTableItemInfo (for C++Builder: __classid(TRVTableItemInfo)).

Return value: True if there is item of the given class at the position of caret (i.e. the caret is to the left or to the right of table), or if the caret is inside item the given class (i.e. the caret is in table cell).

Item receives the top-level item of the given class (i.e. table).

ItemRichViewEdit receives the parent editor for Item  (this Item is in this editor).

This editor can be "root" RichViewEdit or cell inplace editor.

Example

// MyRichViewEdit:TRichViewEdit is an editor

// placed on the form at design time.

// Note: the most of operations are performed in

// rve (editor returned by GetCurrentItemEx),

// not in MyRichViewEdit.

var item: TCustomRVItemInfo;

    table: TRVTableItemInfo;

    Data: Integer;

    rve: TCustomRichViewEdit;

    ItemNo: Integer;

begin

  if not MyRichViewEdit.CanChange or

     not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, rve, 

       item) then

    exit;

  table := TRVTableItemInfo(item);

  ItemNo := rve.GetItemNo(table);

  rve.BeginItemModify(ItemNo, Data);

  // performing some operation, for example

  // table.InsertRowsBelow(1);

  // or table.CellPadding := 10

  rve.EndItemModify(ItemNo, Data);

  rve.Change;

end;