ginsonic
asked on
DBGrid and DataSource
I need to hook before and afterscroll events in my CustomDBGrid. Something alike :
DataLink.DataSet.BeforeScr oll:= MyProc1;
DataLink.DataSet.AfterScro ll := MyProc2;
I try to do this in Create procedure but look like DataSource isn't created. Yet .
Where can I do that ?
DataLink.DataSet.BeforeScr
DataLink.DataSet.AfterScro
I try to do this in Create procedure but look like DataSource isn't created. Yet .
Where can I do that ?
ASKER
No . I refer to CustomDBGrid procedure . Else was to easy :)
Alternately you might do something like this:
program Project2;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {CustomerData: TDataModule};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TFo rm1, Form1);
Application.CreateForm(TCu stomerData , CustomerData);
CustomerData.DataLink.Data set.AfterS croll:=Uni t2.Proc1;
CustomerData.DataLink.Data set.Before Scroll:=Un it2.Proc2;
Application.Run;
end.
Good luck!!
program Project2;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {CustomerData: TDataModule};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TFo
Application.CreateForm(TCu
CustomerData.DataLink.Data
CustomerData.DataLink.Data
Application.Run;
end.
Good luck!!
ASKER
I maked a transparent DBGrid . Work fine for a single DBGrid .
But when I use 2...n grids for same table , I have problems with painting. So I need to handle this events to force the refresh .
But when I use 2...n grids for same table , I have problems with painting. So I need to handle this events to force the refresh .
aha... So why don't you scope the DataSet globally and create them in the constructor of the CustomDBGrid?
Example:
dsQry:TQuery;
Procedure Proc1;
Procedure Proc2;
/// constructor of CustomDBGrid
begin
DsQry:=TQuery.create;
With DsQry do
begin
BeforeScroll:=Proc1;
AfterScroll:=Proc2;
end;
Inherited;
end;
Good luck!!
Example:
dsQry:TQuery;
Procedure Proc1;
Procedure Proc2;
/// constructor of CustomDBGrid
begin
DsQry:=TQuery.create;
With DsQry do
begin
BeforeScroll:=Proc1;
AfterScroll:=Proc2;
end;
Inherited;
end;
Good luck!!
ASKER
Don't work :(
The Proc1 and Proc2 aren't used .
The Proc1 and Proc2 aren't used .
Look, quick example:
procedure TForm1.Button1Click(Sender : TObject);
var Qry:Tquery;
begin
Qry:=TQuery.create(self);/ /create a dataset
With Qry do
begin
AfterScroll:=MyAfterscroll ; //assign the event handler
Databasename:='Tracs';
Sql.clear;
Sql.add('Select * from OriginCodes');
Open;
end;
end;
procedure TForm1.MyAfterScroll(DataS et: TDataSet);
begin
ShowMessage('I''ve been scrolled');//just a message in the event
end;
This works.... what are you doing?
procedure TForm1.Button1Click(Sender
var Qry:Tquery;
begin
Qry:=TQuery.create(self);/
With Qry do
begin
AfterScroll:=MyAfterscroll
Databasename:='Tracs';
Sql.clear;
Sql.add('Select * from OriginCodes');
Open;
end;
end;
procedure TForm1.MyAfterScroll(DataS
begin
ShowMessage('I''ve been scrolled');//just a message in the event
end;
This works.... what are you doing?
ASKER
Can you put these line into a CustomDBGrid ?
And I need to use the specified DataSource into component property .
I'm interested to know in DBGrid when the user navigate through table .
I will increase to 100 points .
Thanks for support,
Nick
And I need to use the specified DataSource into component property .
I'm interested to know in DBGrid when the user navigate through table .
I will increase to 100 points .
Thanks for support,
Nick
a very simple example that does what you're asking:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,dbgrids,db,dbtabl es;
Type
TmyGrid=class(TCustomDBGri d)
ds:TDataSource;
Qry:TQuery;
Private
protected
Procedure BScroll(DataSet:TDataset);
Procedure AScroll(DataSet:TDataSet);
public
Constructor Create(Aowner:TComponent); override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyGrid:TmyGrid;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
{ TmyGrid }
procedure TmyGrid.AScroll(DataSet: TDataSet);
begin
ShowMessage('Before Scroll');
end;
procedure TmyGrid.BScroll(DataSet: TDataset);
begin
ShowMessage('After Scroll');
end;
constructor TmyGrid.Create(Aowner: TComponent);
begin
inherited;
ds:=TDataSource.Create(sel f);
Qry:=TQuery.Create(self);
with Qry do
begin
Databasename:='Tracs';
Sql.clear;
AfterScroll:=ASCroll;
BeforeScroll:=BScroll;
Sql.Add('Select * from OriginCodes');
end;
ds.Dataset:=Qry;
self.DataSource:=ds;
Qry.Open;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
MyGrid:=TMyGrid.Create(sel f);
MyGrid.Parent:=self;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,dbgrids,db,dbtabl
Type
TmyGrid=class(TCustomDBGri
ds:TDataSource;
Qry:TQuery;
Private
protected
Procedure BScroll(DataSet:TDataset);
Procedure AScroll(DataSet:TDataSet);
public
Constructor Create(Aowner:TComponent);
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyGrid:TmyGrid;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
{ TmyGrid }
procedure TmyGrid.AScroll(DataSet: TDataSet);
begin
ShowMessage('Before Scroll');
end;
procedure TmyGrid.BScroll(DataSet: TDataset);
begin
ShowMessage('After Scroll');
end;
constructor TmyGrid.Create(Aowner: TComponent);
begin
inherited;
ds:=TDataSource.Create(sel
Qry:=TQuery.Create(self);
with Qry do
begin
Databasename:='Tracs';
Sql.clear;
AfterScroll:=ASCroll;
BeforeScroll:=BScroll;
Sql.Add('Select * from OriginCodes');
end;
ds.Dataset:=Qry;
self.DataSource:=ds;
Qry.Open;
end;
procedure TForm1.Button1Click(Sender
begin
MyGrid:=TMyGrid.Create(sel
MyGrid.Parent:=self;
end;
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ITugay , look like your idea is the best for me . One question, when I override the Scroll procedure I don't affect anything ? I see that the vcl procedure contain a lots of lines . I must add these to my overrided procedure ?
Sorry for my stupid question , but I'm an amateur programmer and still learning .
Regards,
Nick
P.S. I tested with Loaded too, but no good result .
Sorry for my stupid question , but I'm an amateur programmer and still learning .
Regards,
Nick
P.S. I tested with Loaded too, but no good result .
Hi ginsonic,
there should be no any problems with Scroll overriding, just do not forget to call "Inherited" method first.
Something like this:
TSomeGrid = class(....)
procedure Scroll(D : integer); override;
end;
....
procedure TSomeGrid.Scroll(D : integer);
begin
Inherited;
//
// do your stuffs here
//
end;
------
Igor.
there should be no any problems with Scroll overriding, just do not forget to call "Inherited" method first.
Something like this:
TSomeGrid = class(....)
procedure Scroll(D : integer); override;
end;
....
procedure TSomeGrid.Scroll(D : integer);
begin
Inherited;
//
// do your stuffs here
//
end;
------
Igor.
ASKER
Sorry for wrong information.
In fact Scroll is called after BeforeScroll event . Not good .
In fact Scroll is called after BeforeScroll event . Not good .
ASKER
DrDelphi , in your code you specified the name of database. I don't know the used name :( or the used tables .
ASKER
OK Igor . Finally have found a way . Using your idea of course . So take the points :)
ASKER
Increased to 100 points, how I promise .
ASKER
With Scroll I can hook now the table navigation except when press Home or End keys .
Look like First and Last don't use Scroll procedure . Any idea how to hook this events ? I have try with KeyDown but work just for the focused DBGrid :(
Offer anothers 50 points.
Thanks in advance,
Nick
ASKER
Here is the link for the second question :
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20269544
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20269544
hi ginsonic,
>> Look like First and Last don't use Scroll procedure.
yes, it is. I will try to do something with it.
btw, there is WM_PAINT message. It seems like it appears after dataset go to the First or Last row.
And one more, why don't you use WM_PAINT message to perform your own drawing?
-----
Igor.
>> Look like First and Last don't use Scroll procedure.
yes, it is. I will try to do something with it.
btw, there is WM_PAINT message. It seems like it appears after dataset go to the First or Last row.
And one more, why don't you use WM_PAINT message to perform your own drawing?
-----
Igor.
ASKER
Can't use it . I use some codes to eliminate the flickering and have problems if I type there these lines .
Good luck!!