Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

resaze DBGrid to fit displayed columns.

Posted on 2001-08-04
4
Medium Priority
?
294 Views
Last Modified: 2010-04-06


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.

0
Comment
Question by:kzdown
  • 3
4 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6352701
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
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6352706
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
0
 

Author Comment

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

thanks
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 165 total points
ID: 6353918
Hello

  try this code, it's work fine with some tables i have, it will resize the column to fit in the dbgrid, I hope that u need

//===
var
  I,W,D : Integer;
  P : Double;
begin
  W :=0;
  for I := 0 to DBGrid1.Columns.Count -1 do
    W := W + DBGrid1.columns[I].Width;
  ShowMessage(IntToStr(W));
  ShowMessage(IntToStr(DBGrid1.Width));
  D := DBGrid1.Width -W;
  P := (D-40)/DBGrid1.Columns.Count;
  for I := 0 to DBGrid1.Columns.Count -1 do
    DBGrid1.columns[I].Width := Trunc(DBGrid1.columns[I].Width + P);
end;


Mohammed
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question