[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Multiple grids in a scrollable window

I am looking to create multiple grids into a scrollable panel/window.

The example I am trying to implement is similar to ms sql server query ananlyzer when there are multiple record sets generated, these display multilpe grids in a scrollable window.

The number of grids will need to be generated on the fly.

Anyone have any ideas how to do this


thanks

Ian
0
alchemy9
Asked:
alchemy9
  • 2
1 Solution
 
vadim_tiCommented:
procedure someform.somemethod;
begin
  with TDBGrid.Create(Self) do begin
     parent := yourPanel;
    // set wanted grid properties
  end;
end;
0
 
geobulCommented:
Hi,

A possible solution is to use one TScrollBox component as a parent of your grids. Create it on the form and set grids parent properties to that box. If you need code, let me know.

Regards, Geo
0
 
esoftbgCommented:
Or TPageControl with many TTabSheets containing DBGrids .... You will not scroll, but browse between TabSheets ....
0
 
esoftbgCommented:
download a full example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21178629.zip

unit Unit1_Q_21178629;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, DB, DBTables, Grids, DBGrids,
  Buttons, StdCtrls, ExtCtrls, DBCtrls, ComObj, ADODB, ToolWin, ComCtrls,
  Spin, Calendar;

type
  TForm1 = class(TForm)
    ToolBar: TToolBar;
    ADOConnection: TADOConnection;
    ListBoxTables: TListBox;
    PageControl: TPageControl;
    tbtnShowTable: TToolButton;
    tbtnDatabase: TToolButton;
    ADOCommand: TADOCommand;
    tbtnDeletePage: TToolButton;
    procedure FormCreate(Sender: TObject);
    procedure tbtnShowTableClick(Sender: TObject);
    procedure PageControlChange(Sender: TObject);
    procedure tbtnDatabaseClick(Sender: TObject);
    procedure ListBoxTablesClick(Sender: TObject);
    procedure tbtnDeletePageClick(Sender: TObject);
    procedure ListBoxTablesDblClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private   { Private declarations }
    ConnStr:  string;
  public    { Public declarations }
    function  OpenAccessDB(FN: string): Boolean;
  end;

var
  Form1: TForm1;

implementation

uses
  Unit2_Q_21178629;

{$R *.dfm}

function  TForm1.OpenAccessDB(FN: string): Boolean;
var
  B:      Boolean;
  T:      string;
  V:      Variant;
  SL:     TStringList;
  function  CreateAccessTable(TableName: string; Fields: string): Boolean;
  begin
    Result := True;
    try
      ADOCommand.Connection := AdoConnection;
      T :=' Create Table ' + TableName
        + ' ( ' + Fields + ' )';
      ADOCommand.CommandText := T;
      ADOCommand.Execute;
    except
      Result := False;
    end;
  end;
begin
  B := True;
  try
    try
      ConnStr := 'Data Source=' + FN + ';Provider=Microsoft.Jet.OLEDB.4.0';
      if not FileExists(FN) then
      begin
        V := CreateOleObject('ADOX.Catalog');
        V.Create(ConnStr);
      end;
      ADOConnection.Connected := False;
      ADOConnection.ConnectionString := ConnStr;
      ADOConnection.Open;
      SL := TStringList.Create;
      try
        AdoConnection.GetTableNames(SL);
      finally
        ListBoxTables.Items.Assign(SL);
        if (ListBoxTables.Items.Count>0) then
          ListBoxTables.ItemIndex := 0;
        SL.Free;
      end;
    except
      B := False;
    end;
  finally
    Result := B;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  FN:     string;
begin
  FN := ExtractFilePath(Application.ExeName) + 'DATABASE.MDB';
  OpenAccessDB(FN);
end;

procedure TForm1.tbtnShowTableClick(Sender: TObject);
var
  B:      Boolean;
  I:      Integer;
  Idx:    Integer;
  SL:     TStringList;
  S:      string;
  T:      string;
  Bar:    TPanel; // TToolBar;
  ADOQ:   TADOQuery;
  DBG:    TDBGrid;
  DBN:    TDBNavigator;
  DSA:    TDataSource;
  Sheet:  TTabSheet;
  ListBox:TListBox;
begin
  Idx := 0;
  if (ListBoxTables.Items.Count>0) then
  if (ListBoxTables.ItemIndex>-1) then
  begin
    B := True;
    S := ListBoxTables.Items[ListBoxTables.ItemIndex];
    for I := 0 to PageControl.PageCount-1 do
    begin
      if (PageControl.Pages[I].Caption=S) then
      begin
        Idx := ListBoxTables.ItemIndex;
        B := False;
        Break;
      end;
    end;
    if B then
    begin
      Idx := PageControl.PageCount;
      Sheet := TTabSheet.Create(PageControl);
      Sheet.PageControl := PageControl;
      Sheet.Caption := S;
      ListBox := TListBox.Create(Sheet);
      ListBox.Parent := Sheet;
      SL := TStringList.Create;
      try
        Bar := TPanel.Create(Sheet);
        Bar.Parent := Sheet;
        ADOQ := TADOQuery.Create(Sheet);
        AdoConnection.GetFieldNames(S, SL);
        ListBox.Items.Assign(SL);
        ADOQ.Connection := AdoConnection;
        T :=''
          + ' SELECT * FROM ' + S
          + '';
        ADOQ.SQL.Text := T;
        DSA := TDataSource.Create(Sheet);
        DSA.DataSet := ADOQ;
        DBG := TDBGrid.Create(Sheet);
        DBG.Parent := Sheet;
        DBG.DataSource := DSA;
        DBN := TDBNavigator.Create(Sheet);
        DBN.Parent := Sheet;
        DBN.DataSource := DSA;
        DBG.Height := 256;
        DBG.Align := alTop;
        ListBox.Align := alClient;
        Bar.Height := 26;
        Bar.Align := alTop;
        Bar.Visible := True;
        ADOQ.Active := True;
      finally
        SL.Free;
      end;
    end;
    PageControl.ActivePageIndex := Idx;
  end;
end;

procedure TForm1.ListBoxTablesDblClick(Sender: TObject);
begin
  tbtnShowTableClick(Sender);
end;

procedure TForm1.PageControlChange(Sender: TObject);
var
  I:      Integer;
  S:      string;
begin
  S := PageControl.Pages[PageControl.ActivePageIndex].Caption; // ;
  for I := 0 to PageControl.PageCount-1 do
  begin
    if (ListBoxTables.Items[I]=S) then
    begin
      ListBoxTables.ItemIndex := I;
      Break;
    end;
  end;
end;

procedure TForm1.ListBoxTablesClick(Sender: TObject);
var
  I:      Integer;
  S:      string;
begin
  S := ListBoxTables.Items[ListBoxTables.ItemIndex];
  for I := 0 to PageControl.PageCount-1 do
  begin
    if (PageControl.Pages[I].Caption=S) then
    begin
      PageControl.ActivePageIndex := I;
      Break;
    end;
  end;
end;

procedure TForm1.tbtnDatabaseClick(Sender: TObject);
begin
  Form2.ShowModal;
end;

procedure TForm1.tbtnDeletePageClick(Sender: TObject);
var
  I:      Integer;
begin
  if PageControl.ActivePageIndex>=0 then
  try
    for I := ComponentCount-1 downto 0 do
    begin
      if (Components[I] is TADOQuery) then
      begin
        if ((Components[I] as TADOQuery).Owner=PageControl.Pages[PageControl.ActivePageIndex]) then
        begin
          (Components[I] as TADOQuery).Active := False;
          (Components[I] as TADOQuery).Destroy;
        end;
      end
      else
      if (Components[I] is TControl) then
        if ((Components[I] as TControl).Parent=PageControl.Pages[PageControl.ActivePageIndex]) then
          (Components[I] as TControl).Destroy;
    end;
  finally
    PageControl.Pages[PageControl.ActivePageIndex].Destroy;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  I:      Integer;
begin
  try
    for I := 0 to ADOConnection.DataSetCount-1 do
      ADOConnection.DataSets[I].Active := False;
  finally
    with PageControl do
    begin
      while (PageCount>0) do
      try
        for I := ComponentCount-1 downto 0 do
        begin
          if (Components[I] is TControl) then
            if ((Components[I] as TControl).Parent=Pages[PageCount-1]) then
              (Components[I] as TControl).Destroy;
        end;
      finally
        Pages[PageCount-1].Destroy;
      end;
    end;
  end;
end;

end.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now