Solved

resaze DBGrid to fit displayed columns.

Posted on 2001-08-04
4
248 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 55 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…

832 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