Adding rows to an existing table

General TRichView support forum. Please post your questions here
Post Reply
r1d2m3
Posts: 13
Joined: Sun Mar 07, 2010 12:13 am

Adding rows to an existing table

Post by r1d2m3 »

Sergey, I pose the following query:
I have a document that contains a table which I will add rows as needed, reviewing the documentation, you should use table.InsertRowsBelow (nroRows), the question is: how to reference the table object to use InsertRowsBelow, and that the object was created when creating the template code.?

Thanks.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Table.InsertRowsBelow inserts rows below the selected cells.
To insert in the specified position, use table.InsertRows.

How to get a table object? It depends on which table you need.
Do you need the first table in the document?
Do you need the table at the caret position?
r1d2m3
Posts: 13
Joined: Sun Mar 07, 2010 12:13 am

Post by r1d2m3 »

Thanks Sergey, could be the first or second table, a table put the title first and a second table, the headers of a list.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Sorry, I do not understand your answer.
However, I'll give some examples.

Example 1
How to add a row to the end of the current table.
("a current table" is a table at the caret position; the caret may be to the left or to the right of this table, or inside its cell; or the caret may be hidden but several cells of this table are selected).
The example assumes that the table is inserted in RichViewEdit1.
This is an editing operation, it can be undone by the user.

Code: Select all

var table: TRVTableItemInfo;
    Data: Integer;
    rve: TCustomRichViewEdit;
    ItemNo: Integer;
begin
  if RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, rve, TCustomRVItemInfo(table)) then begin
    ItemNo := rve.GetItemNo(table);
    rve.BeginItemModify(ItemNo, Data);
    table.InsertRows(table.RowCount, 1, table.RowCount-1, False);
    rve.EndItemModify(ItemNo, Data);
    rve.Change;
  end;
end;
Example of other table operations (adding/deleting rows and columns, merging and unmerging cells) can be found in the Demos\Editors\Editor 1\ demo, menu "Table"
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Example 2

Adding a row to the end of the first table in the document.

Code: Select all

// TableNo - index of table. 0 for the first table, 1 for the second, and so on
function GetTableItemNo(RVData: TCustomRVData; TableNo: Integer): Integer;
var i: Integer;
begin
  Result := -1;
  for i := 0 to RVData.ItemCount-1 do
    if RVData.GetItemStyle(i)=rvsTable then begin
      if TableNo=0 then begin
        Result := i;
        exit;
      end;
      dec(TableNo);
    end;
end;

var table: TRVTableItemInfo;
    Data: Integer;
    ItemNo: Integer;
begin
  ItemNo := GetTableItemNo(RichViewEdit1.RVData, 0);
  if ItemNo<0 then
    exit;
  table := TRVTableItemInfo(RichViewEdit1.GetItem(ItemNo));
  RichViewEdit1.BeginItemModify(ItemNo, Data);
  table.InsertRows(table.RowCount, 1, table.RowCount-1, False);
  RichViewEdit1.EndItemModify(ItemNo, Data);
  RichViewEdit1.Change;
end;
This is an editing operation, it can be undone by user.

Example 3
The same code for TRichView instead of TRichViewEdit.

Code: Select all

var table: TRVTableItemInfo;
    ItemNo: Integer;
begin
  ItemNo := GetTableItemNo(RichView1.RVData, 0);
  if ItemNo<0 then
    exit;
  table := TRVTableItemInfo(RichView1.GetItem(ItemNo));
  table.InsertRows(table.RowCount, 1, table.RowCount-1, False);
  RichView1.Format;
end;
r1d2m3
Posts: 13
Joined: Sun Mar 07, 2010 12:13 am

Post by r1d2m3 »

Example 3 me solve my requirement.

Thanks Sergey.
Post Reply