Page 1 of 1

Adding rows to an existing table

Posted: Wed Oct 20, 2010 5:35 pm
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.

Posted: Thu Oct 21, 2010 1:54 pm
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?

Posted: Thu Oct 21, 2010 6:58 pm
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.

Posted: Sat Oct 23, 2010 8:24 am
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"

Posted: Sat Oct 23, 2010 8:33 am
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;

Posted: Sat Oct 23, 2010 8:31 pm
by r1d2m3
Example 3 me solve my requirement.

Thanks Sergey.