Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

resaze DBGrid to fit displayed columns.

Posted on 2001-08-04
4
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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