whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

34 Visitors Online


 
...change the column width in TDBGrid automatically?
Autor: Uwe Braune
Homepage: http://www.dblinks.de
[ Print tip ]  

Tip Rating (7):  
     


(*
--- english -------------------------------------------------------------------
This will change (reduce or extend) the width of column(s) so they all
fit in the Grid.
If aChangeAll = False then only the last col will be modified.
--- german --------------------------------------------------------------------
Anpassung (Vergrößerung oder Verkleinerung) der Spaltenbreite(n).
Wenn aChangeAll = False, dann wird nur die letzte Spalte verändert.
*)
procedure ChangeColumnWidths(DBGrid: TDBGrid; aChangeAll: boolean);
var
  
NewWidth,
  TotalColumnWidth,
  ColumnCount,
  GridClientWidth,
  StepWidth,
  i: integer;
begin
  
ColumnCount := DBGrid.Columns.Count;
  if ColumnCount = 0 then Exit;

  // Ermitteln der momentanen Breite des Grids
  // look for the actual size of all columns
  
TotalColumnWidth := 0;
  for i := 0 to ColumnCount-1 do
    
TotalColumnWidth := TotalColumnWidth + DBGrid.Columns[i].Width;
  if dgColLines in DBGrid.Options then
    
TotalColumnWidth := TotalColumnWidth + ColumnCount;

  // Ermitteln der max. Breite des Grids
  // What's the total Grid width?
  
GridClientWidth := DBGrid.Width - GetSystemMetrics(SM_CXVSCROLL);
  if dgIndicator in DBGrid.Options then begin
    
GridClientWidth := GridClientWidth - IndicatorWidth;
    if dgColLines in DBGrid.Options then
      
Dec(GridClientWidth);
  end;
  if DBGrid.BorderStyle = bsSingle then begin
    if 
DBGrid.Ctl3D then // 2 * 2 Pixel
      
GridClientWidth := GridClientWidth - 4
    else // 2 * 1 Pixel
      
GridClientWidth := GridClientWidth - 2;
  end;

  // Neue Spaltenbreiten setzen: vergößern
  // change the width(s): extend size/width
  
if TotalColumnWidth < GridClientWidth then begin
    
StepWidth := (GridClientWidth - TotalColumnWidth) div ColumnCount;
    // Alle Spalten anpassen
    // for all columns
    
if aChangeAll then begin
      for 
i := 0 to ColumnCount-1 do
        
DBGrid.Columns[i].Width := DBGrid.Columns[i].Width + StepWidth;
    // Nur letzte Splate anpassen
    // only for the last one
    
end else
      
DBGrid.Columns[ColumnCount-1].Width := DBGrid.Columns[ColumnCount-1].Width + ColumnCount * StepWidth;
  end

  
// Neue Spaltenbreiten setzen: verkleinern
  // change the width(s): reduce size/width
  
else if TotalColumnWidth > GridClientWidth then begin
    
StepWidth := (TotalColumnWidth - GridClientWidth) div ColumnCount;
    if (TotalColumnWidth - GridClientWidth) mod ColumnCount <> 0 then Inc(StepWidth);
    // Alle Spalten anpassen
    // for all columns
    
if aChangeAll then begin
      for 
i := 0 to ColumnCount-1 do begin
        
NewWidth := DBGrid.Columns[i].Width - StepWidth;
        if (NewWidth > 5) then DBGrid.Columns[i].Width := NewWidth;
      end;
    // Nur letzte Splate anpassen
    // only for the last one
    
end else begin
      
NewWidth := DBGrid.Columns[ColumnCount-1].Width - ColumnCount * StepWidth;
      if (NewWidth > 5) then DBGrid.Columns[ColumnCount-1].Width := NewWidth;
    end;
  end;

end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners