Dbgrid每页最多显示10条记录,按“→”键向下翻页,按“←”向上翻页。
解决方案 »
- 修改系统日期,看有问题吗??
- 在DBGRID里面鼠标滚动时,如果让与数据库关联的DBEDIT的值也跟着变动.
- DBGrid能不能实现这个?
- TV树中重复的点击内容后,会出现相同的内容,怎样解决这个问题!
- 怎么样对Tchar控件进行预览显示
- 救命啊!!!!
- delphi中的图标问题
- DELPHI BULK INSERT 发生大容量插入数据转换错误(截断)
- 我正在使用ado编写程序请问对于vfp的表或数据库我应选哪个ole db的提供者
- MTS,我应该到哪里去找你??
- delphi利用XMLDocument实现对xml文件的增加、删除节点操作
- 在线请教:ScrollWindowEx 为啥总有几次不成功!?
2、
private
OldGridProc: TWndMethod;
procedure GridWindowProc(var Message: TMessage);
//...procedure TForm1.FormCreate(Sender: TObject);
begin
OldGridProc := DBGrid1.WindowProc;
DBGrid1.WindowProc := GridWindowProc;
end;procedure TForm1.GridWindowProc(var Message: TMessage);
var
Pos: SmallInt;
begin
OldGridProc(Message);
if Message.Msg = WM_VSCROLL then
begin
Pos := Message.WParam;
Table1.RecNo := Pos;
end;
end;
begin
if Key=#37 then
dbgrd1.Perform(WM_VSCROLL,SB_PAGEUP,0);
if Key=#39
dbgrd1.Perform(WM_VSCROLL,SB_PAGEDOWN,0);
end;
Query1.SQL.Text := 'select max(id) as maxid, min(id) as minid from TB1 ;
Query1.Open;
在点击ButtonNext按钮中,每点击一次计算一次最大bid
Query1.SQL.Text := 'select top 10 * from TB1 where id < bid order by id desc';
在点击ButtonPrev按钮中,每点击一次计算一次最小tid
'select * from (select top 10 * from TB1 where id > tid order by id asc) t order by id desc';DBGrid.DataSource.DataSet := Query1;procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key=39 then
ButtonNext.Click;
if Key=37 then
ButtonPrev.Click;
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, Grids, DBGrids;type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
procedure FormCreate(Sender: TObject);
procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
OldGridProc: TWndMethod;
procedure GridWindowProc(var Message: TMessage);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
OldGridProc := DBGrid1.WindowProc;
DBGrid1.WindowProc := GridWindowProc;
end;procedure TForm1.GridWindowProc(var Message: TMessage);
var
Pos: SmallInt;
begin
OldGridProc(Message);
if Message.Msg = WM_VSCROLL then
begin
Pos := Message.WParam;
Table1.MoveBy(Pos*message.LParam);//這么做,是為了使得dataset指標跟著同步移動
end;
end;procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_LEFT:
begin
SendMessage(DBGrid1.Handle,WM_VSCROLL, SB_PAGEUP,-1);
Key := 0;//琂礛オ龄璶癬page up and down狦碞ぃ琵膥尿矪瞶
end;
VK_Right:
begin
SendMessage(DBGrid1.Handle,WM_VSCROLL, SB_PAGEDOWN,1);
Key := 0;
end;
end;
end;end.
另外,“Dbgrid每页最多显示10条记录” ——是否可理解為12樓的分頁查詢呢?還是你的數據已經一次性取下來了,只是要翻頁而已?
var
HitEOF: Boolean;
begin
if InFetchData or AlreadyFetchAll then
Exit; InFetchData := True;
//鎖定UI,避免用戶誤操作
Form1.Enabled := False;
Screen.Cursor := crSQLWait;
try
cdsFetch.Close;
SQL := Format('SELECT TOP %d ITEM001, ITEM002 FROM WD2ITEM', [RecCountPerPage]); if (ParamValue = '') then
cdsFetch.CommandText := Format('%s WHERE %s>%s ORDER BY %s',
[SQL, PrimaryKeys, QuotedStr(LastPrimaryKeyValues), PrimaryKeys])
else
cdsFetch.CommandText := Format('%s WHERE %s>%s AND %s LIKE %s ORDER BY %s',
[SQL, PrimaryKeys, QuotedStr(LastPrimaryKeyValues), SearchField,
QuotedStr(Format('%%%s%%', [ParamValue])), PrimaryKeys]); Log(cdsFetch.CommandText);
cdsFetch.Open;
cdsFetch.Last;
LastPrimaryKeyValues := cdsFetch[PrimaryKeys];
//調用GridView的BeginUpdate,以避免更新紀錄時,造成其他事件被觸發
cxGrid1DBTableView1.BeginUpdate;
cdsData.AppendData(cdsFetch.Data, HitEOF);
AlreadyFetchAll := (cdsFetch.RecordCount < RecCountPerPage);
cxGrid1DBTableView1.Controller.FocusedRecordIndex := cxGrid1DBTableView1.Controller.TopRecordIndex;
cxGrid1DBTableView1.EndUpdate;
finally
cdsFetch.Close;
Screen.Cursor := crDefault;
InFetchData := False;
//解除UI鎖定
Form1.Enabled := True;
end; Log('### Fetch data ###');
end;