Link to home
Start Free TrialLog in
Avatar of kzdown
kzdown

asked on

resaze DBGrid to fit displayed columns.



I want to make dbgrid(also parent form) to fit columns shown there.
i have followng code :
=====================================================================
procedure TTableForm.FormShow(Sender: TObject);
var
  www,i :Integer;
begin
      www:=0;
      for I := 0 to DBGrid1.Columns.Count - 1 do    // Iterate
        begin
          www:=www+dbgrid1.Columns[i].Width;
        end;    // for
      if www<500 then
        width:=www+50 // 50 is for scrollbar and dbIndicator column
      else
        width:=500;
end;
=====================================================================

but seems it isnt work ok.
when this procedure occurs database table is already opened, so it should count columnt widths and add 50 pixels for scrollbar and indicator column. but somethng is wrong. On some table it shows more space than needed, and on some oter table it shows less width then needed. How to make this dbgrid(form) to fit exactly to displayed dbgrid clumns?

Does anyone knows how to help me?

thanks in advance.

Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Hello

  Hello when u r using Width only, it's mean the form width, so you have to use DBGrid1.width

your code will work fine as following:

procedure TTableForm.FormShow(Sender: TObject);
var
 www,i :Integer;
begin
  www:=0;
  for I := 0 to DBGrid1.Columns.Count - 1 do    // Iterate
    www:=www+dbgrid1.Columns[i].Width;
  DBGrid1.Width := www + 50;
end;

Mohammed
if you want to use ur code
just change the less than with greeter than in the if statment

use this code

     if www>500 then
       DBGRid1.width:=www+50 // 50 is for scrollbar and dbIndicator column
     else
       DBGrid1.width:=500;

instead of this code

if www<500 then
       width:=www+50 // 50 is for scrollbar and dbIndicator column
     else
       width:=500;

Mohammed
Avatar of kzdown
kzdown

ASKER

dbgrid is aligned to fit form,
but however
it won;t work even for dbgrid.width.
pls try.

thanks
ASKER CERTIFIED SOLUTION
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial