Delphi Pages Forums

Delphi Pages Forums (
-   DB-Aware (
-   -   How to add data to dynamically created Frame's DBGrid (

kbboykin 10-24-2007 06:19 AM

How to add data to dynamically created Frame's DBGrid
Good morning all,

I am working on an app that creates dynamic tabsheets to be added to static pagecontrol at run time. The tabsheets are complex and contain a frame that is also dynamically docked on tabsheet. Each sheet represents a different record up to five. I am able to get the tabsheets to contain the right info for each record from DBEdits, but when I try to use the DBGrid with each new record the contents change for ALL open records DBGrid to the info on the last open record. In other words if I open Patient A, his records and DBGrid are all correct, but then if I open Patient B while Patient A is still open, the dbedits are correct for patient A, but his DBGrid now contains Patient B data. Here's my code so far:

unit mainfrm;


Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ComCtrls, XPMan, ExtCtrls, StdCtrls, Buttons;

TfrmMain = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Maintenance1: TMenuItem;
N1: TMenuItem;
PrinterSetup1: TMenuItem;
N2: TMenuItem;
Exit1: TMenuItem;
XPManifest1: TXPManifest;
StatusBar1: TStatusBar;
Panel1: TPanel;
btnHome: TBitBtn;
btnGetChart: TBitBtn;
btnEnterVax: TBitBtn;
btnShutDown: TBitBtn;
PgCtrl: TPageControl;
tsHome: TTabSheet;
procedure Exit1Click(Sender: TObject);
procedure btnGetChartClick(Sender: TObject);
{ Private declarations }
{ Public declarations }
procedure CreatePatientTabSheet;
procedure DynaButtonClick(Sender: TObject);

frmMain: TfrmMain;


uses ptFrame, datamod, ptlocatefrm;

{$R *.dfm}

procedure TfrmMain.Exit1Click(Sender: TObject);

procedure TfrmMain.DynaButtonClick(Sender: TObject);

procedure TfrmMain.btnGetChartClick(Sender: TObject);
if ptdata.patients.Active = false then ptdata.patients.Active := True;
PgCtrl.ActivePageIndex := PgCtrl.PageCount - 1;

procedure TfrmMain.CreatePatientTabSheet;
if frmMain.PgCtrl.PageCount <= 5 then
with TTabSheet.Create(self) do
PageControl := PgCtrl;
Name := '';
TabVisible := True;
PgCtrl.TabWidth := 230;
Caption := ptdata.patientsPTUID.Value + ' - ' + ptdata.patientsptname.Value ;
ParentBackground := False;
{with TPanel.Create(self) do
Width := 230;
Align := alLeft;
Name := '';
Parent := PageControl.Pages[PageControl.PageCount -1];
Color := clGray;
ParentBackground := False;
with TBitBtn.Create(self) do
Name := '';
Kind := bkCustom;
Parent := PageControl.Pages[PageControl.PageCount -1];
Caption := '&Close Chart';
Top := PageControl.Pages[PageControl.PageCount -1].Height - 30;
Height := 30;
width := 230;
OnClick := DynaButtonClick;
with TPatientFrame.Create(self) do
pageControl1.ActivePage := tabsheet1;
//PageControl1.Height :=
Name := '';
Parent := PageControl.Pages[PageControl.PageCount -1];
Align := alClient;
Bitbtn3.Top := PageControl.Pages[PageControl.PageCount -1].Height - 30;
{ Overview Tabsheet Data}
DBEdit1.Text := ptdata.patientsFullName.Value;
DBEdit2.Text := ptdata.patientsaddress.Value;
DBEdit3.Text := ptdata.patientscitystzip.Value;

{ Demographics Tab Data}
PageControl2.ActivePageIndex := 0;
DBEdit4.Text := ptdata.patientsCHARTNO.Value;
DBEdit5.Text := ptdata.patientsLASTNAME.Value;
DBEdit6.Text := ptdata.patientsFIRSTNAME.Value;
DBEdit7.Text := ptdata.patientsMIDDLEINIT.Value;
DBEdit8.Text := ptdata.patientsDOB.AsString;

{ Immunization Grid Data}
//imActiveSource.DataSet := ptdata.vaxhx;
//DBGrid3.Columns[8].FieldName := ptdata.vaxhxPTUID.Value;
//DBGrid3.Columns[0].FieldName := ptdata.vaxhxVAXNAME.Value;

end else
ShowMessage('You have too many charts currently open.'+#13+#13+'You need to close at least one chart.');


Any help appreciated.


digitiger 08-20-2009 10:57 AM

use different dataset for all tabs
Yes, because currently all your grids point to the same dataset for which Delphi has only one pointer. So if on Tab "A" you move cursor pointer to last record then that gets reflected in all controls (and grids) because they all are synced with same dataset/datasource.

If you wish to have all the tabs/grids showing their own different information then either you will have to populate/manipulate data in static controls like StringGrid which might be like killing a fly with sword. Alternatively you may use a new copy of dataset/datasource for each of the DBGrid so that they all are not synced with each other or same dataset.

NB: just glanced at your source and first few lines seems that you are terminating application. Dont ever terminate an application especially if it is having database connections or is using system resources or most importantly if you are creating components dynamically because it will cause orphan locks or leaking memory! Instead close the main form of the application which will eventually close the application by itself but before that do close all open database connection and delete any dynamically created components.

Hope it helps.

All times are GMT. The time now is 06:47 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.