Row cloning

General TRichView support forum. Please post your questions here
Post Reply
Nofate
Posts: 11
Joined: Thu Feb 14, 2008 11:11 am

Row cloning

Post by Nofate »

I have the following problem:
- I need to walkthrough a table
- if table has any row containing text of a defined color (i.e. $0000ff) - duplicate this row.

Can you suggest any working solution?

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

Post by Sergey Tkachenko »

Do you want to add a new row in the same table? After the source row or to the end? Should it be an editing operation (undoable) or not?
Nofate
Posts: 11
Joined: Thu Feb 14, 2008 11:11 am

Post by Nofate »

Yes, I want to insert row into the same table just after the source row.
It shouldn't be an editing operation.
And I can't get idea how to get color of the text fragment in cell.
Nofate
Posts: 11
Joined: Thu Feb 14, 2008 11:11 am

Post by Nofate »

May be it will be easier to suggest a solution if i describe general task.
I need to fill table having only one row with tags like "[name]","[job]" etc with values from dataset.
Nofate
Posts: 11
Joined: Thu Feb 14, 2008 11:11 am

Post by Nofate »

May be it will be easier to suggest a solution if i describe general task.
I need to fill table having only one row containing tags like "[name]","[job]" etc with values from dataset.
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Table has undocumented methods SaveRowsToStream and LoadFromStreamEx.
You can use them to copy rows:

Code: Select all

var Stream: TMemoryStream;
   Row: Integer;
begin
  Row := 0;
  Stream := TMemoryStream.Create;
  table.SaveRowsToStream(Stream, Row, 1);
  Stream.Position := 0;
  table.InsertRows(Row+1, 1, 0, False);
  table.LoadFromStreamEx(Stream, Row+1);
  Stream.Free;
Note that if table is inserted in the editor, InsertRows becomes and editing operation. So call the code before inserting the table, or call ClearUndo when finished.
As for recognizing if the row has cell having text of the given color:

Code: Select all

function RowHasTextColor(RVStyle: TRVStyle; table: TRVTableItemInfo; Row: Integer; Color: TColor): Boolean;
var c, i, StyleNo: Integer;
begin
  Result := True;
  for c := 0 to table.ColCount-1 do
    if table.Cells[Row,c]<>nil then
      for i := 0 to table.Cells[Row,c].GetRVData.ItemCount-1 do begin
        StyleNo := table.Cells[Row,c].GetRVData.GetItemStyle(i);
        if (StyleNo>=0) and (RVStyle.TextStyles[StyleNo].Color=Color) then
          exit; // found
      end;
  Result := False;
end;
Nofate
Posts: 11
Joined: Thu Feb 14, 2008 11:11 am

Post by Nofate »

Thanks
Nofate
Posts: 11
Joined: Thu Feb 14, 2008 11:11 am

Post by Nofate »

LoadFromStreamEx() raises exception EReadError with message 'Wrong end of cells list' in method TRVTableItemInfo.CellsReader() after "if not Reader.EndOfList"
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Probably you copy to the row having different number of cells. Probably because of cell merging.
Post Reply