Code To Widen Last Column Doesn't Work

General TRichView support forum. Please post your questions here
Post Reply
fchintc
Posts: 47
Joined: Thu Oct 25, 2007 2:49 pm

Code To Widen Last Column Doesn't Work

Post by fchintc »

Hi,

I have the following table:-

Image

I want the last column of the table to be widened up to the right margin of the page so that it looks like the following:-

Image

However, using the following code, I cannot get the last column to widen:-

Code: Select all

table.BestWidth:=-100;
for nX := 0 to table.RowCount-1 do begin
     if table.Cells[nX, table.ColCount-1]<>nil then begin
        table.Cells[nX, table.ColCount-1].BestWidth := 0;  
     end;
end;
After the above code is run, I get the following printout:-

Image

If I manually resize the table like the following, I will get the printout I want:-

Image

How can I get the code to work?

Thanks.

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

Post by Sergey Tkachenko »

The code I gave before reset width of cells in the last column. But your table is more complex, it has cells in previous columns spanning over the last column.
Try this code instead:

Code: Select all

var r,mr,mc: Integer;

table.BestWidth:=-100; 
for r := 0 to table.RowCount-1 do begin 
  table.Rows.GetMainCell(r, table.ColCount-1, mr, mc);   
  table.Cells[mr, mc].BestWidth := 0;  
end; 
fchintc
Posts: 47
Joined: Thu Oct 25, 2007 2:49 pm

Post by fchintc »

Thanks for the code.

The result of the new code is as follows with the right columns of the first two rows still not stretching to the right margin of the page:-

Image

Any other suggestions?

Thanks.

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

Post by Sergey Tkachenko »

I can see several grid lines to the right side of the table, in the first two rows.
It looks like there are one or more empty columns at the end of the table. Size of this empty column is increased, while other columns remain of the same size.
Last edited by Sergey Tkachenko on Tue Sep 30, 2008 3:23 pm, edited 1 time in total.
fchintc
Posts: 47
Joined: Thu Oct 25, 2007 2:49 pm

Post by fchintc »

Sergey Tkachenko wrote:I can see several grid lines to the right side of the table, in the first two rows.
It looks like there are one or more empty columns at the end of the table. Size of this empty column is increased, while other columns remains of the same size.
In that case, the solution may be to determine if the last column for the row is blank. If it is, set the bestwidth for the column before it to 0. How to I determine if a column is blank?

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

Post by Sergey Tkachenko »

Please send me RVF file with this table.
fchintc
Posts: 47
Joined: Thu Oct 25, 2007 2:49 pm

Post by fchintc »

I have sent you the table that is a RTF in a DBISAM BLOB field. Hope that the format is OK. I couldn't get it to export to a RTF file.

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

Post by Sergey Tkachenko »

Ok, these unnecessary columns at the end of the table have small widths, 2 pixels or less.
So, before changing table width, we need to delete these columns.

Code: Select all

var
  r,c,w,mr,mc: Integer;

  for c := table.ColCount-1 downto 0 do begin
    w := 0;
    for r := 0 to table.RowCount-1 do
      if (table.Cells[r,c]<>nil) and (table.Cells[r,c].Width>w) then begin
        w := table.Cells[r,c].Width;
        if w>2 then
          break;
      end;
    if (w>2) or (c=0) then begin
      if (c<table.ColCount-1) then
        table.DeleteCols(c+1, table.ColCount-1-c, False);
      break;
    end;
  end;

  table.BestWidth:=-100;
  for r := 0 to table.RowCount-1 do begin
    table.Rows.GetMainCell(r, table.ColCount-1, mr, mc);
    table.Cells[mr, mc].BestWidth := 0;
  end;
fchintc
Posts: 47
Joined: Thu Oct 25, 2007 2:49 pm

Post by fchintc »

Thank you very much for the code! It works nicely.

Frederick
Post Reply