Solved

resaze DBGrid to fit displayed columns.

Posted on 2001-08-04
4
242 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:mnasman
Comment Utility
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:mnasman
Comment Utility
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
Comment Utility
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:
mnasman earned 55 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now