Autor Beitrag
D. Annies
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 28.01.10 18:15 
Hi, Delpher,

diesesmal experimentiere ich mit der Breite eines (beliebigen) DBGrids, aber es gelingt noch nicht ganz - die Änderung wird ignoriert:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
procedure TForm1.DSGridbreite(Grid: TControl);
var  sg: TStringGrid;
    dbg: TDBGrid;
    n, k, cw : integer;
begin
  if (grid is TDBGrid) then
  begin
    dbg := (grid as TDBGrid);
    dbg.Width := 30;                    // rh := SG.Canvas.TextHeight('Äj');
    for n := 0 to dbg.fieldcount-1 do
    begin
      cw := 0;      // z.B. tb_ausleihsatz
      for k := 0 to dbg.DataSource.DataSet.RecordCount -1 do
      begin
        if dbg.canvas.TextWidth(dbg.fields[n].asstring) > cw then cw := dbg.canvas.TextWidth(dbg.fields[n].asstring);
      end;
      dbg.Width := dbg.Width + cw + 4;
      showmessage('hi');   // um die Größenänderung zu sehen
    end;
    dbg.Width := dbg.Width + 20;
  end;

  if (grid is tStringGrid) then
  begin
    sg := (grid as TStringGrid);  // hier fehlt auch noch etwas ..

  end;

end;


Wer kann da helfen? (Manchmal ist wenig sehr viel)
Gruß, Detlef

_________________
ut vires desint, tamen est laudanda voluntas
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 28.01.10 19:11 
So, nach googeln und ausprobieren / verwerfen habe ich jetzt folgende "Lösung"

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
procedure TForm1.ChangeColumnWidths(DBGrid: TDBGrid);
var  NewWidth,  TotalColumnWidth,  ColumnCount,
     GridClientWidth,  StepWidth,                  i: integer;
begin
  ColumnCount := DBGrid.Columns.Count;
  if ColumnCount = 0 then Exit;

  TotalColumnWidth := 0;  // Ermitteln der momentanen Breite des Grids
  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
  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;

  if TotalColumnWidth < GridClientWidth then
  begin  // Neue Spaltenbreiten setzen: vergößern
    StepWidth := (GridClientWidth - TotalColumnWidth) div ColumnCount;
    for i := 0 to ColumnCount-1 do
      DBGrid.Columns[i].Width := DBGrid.Columns[i].Width + StepWidth;
  end
  else
  if TotalColumnWidth > GridClientWidth then
  begin  // Neue Spaltenbreiten setzen: verkleinern
    StepWidth := (TotalColumnWidth - GridClientWidth) div ColumnCount;
    if (TotalColumnWidth - GridClientWidth) mod ColumnCount <> 0 then Inc(StepWidth);

    for i := 0 to ColumnCount-1 do
    begin
      NewWidth := DBGrid.Columns[i].Width - StepWidth;
      if (NewWidth > 5) then DBGrid.Columns[i].Width := NewWidth;
    end;
  end;
end;


Aber die ersten 3 Spalten werden zu breit und alle anderen zu schmal dargestellt.
Noch nicht der Bringer!
Detlef

_________________
ut vires desint, tamen est laudanda voluntas
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 28.01.10 19:36 
So, jetzt habe ich es:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.DBGridbreite(DBG: TDBGrid);
var n, k, cw : integer;
begin
  dbg.Width := 30;                    // rh := SG.Canvas.TextHeight('Äj');
  for n := 0 to dbg.fieldcount-1 do
  begin
    cw := 0;      // z.B. tb_ausleihsatz
    dbg.DataSource.DataSet.first;
    while not dbg.DataSource.DataSet.Eof do
    begin
      if dbg.canvas.TextWidth(dbg.fields[n].AsString) > cw then cw := dbg.canvas.TextWidth(dbg.fields[n].asstring);
      dbg.Columns[n].Width := cw+2;
      showmessage('hi');   // um die Größenänderung zu sehen
      dbg.DataSource.DataSet.Next;
    end;
    dbg.Width := dbg.Width + cw + 4;
  end;
  dbg.Width := dbg.Width + 20;
end;


So geht's,
Detlef

_________________
ut vires desint, tamen est laudanda voluntas