Solved

Multiple grids in a scrollable window

Posted on 2004-10-22
187 Views
Last Modified: 2010-04-05
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
Question by:alchemy9
    4 Comments
     
    LVL 6

    Expert Comment

    by:vadim_ti
    procedure someform.somemethod;
    begin
      with TDBGrid.Create(Self) do begin
         parent := yourPanel;
        // set wanted grid properties
      end;
    end;
    0
     
    LVL 17

    Expert Comment

    by:geobul
    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
     
    LVL 12

    Expert Comment

    by:esoftbg
    Or TPageControl with many TTabSheets containing DBGrids .... You will not scroll, but browse between TabSheets ....
    0
     
    LVL 12

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…

    857 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

    18 Experts available now in Live!

    Get 1:1 Help Now