delphi下ado+dbgrid实现简单数据集的分页显示

Delphi 专栏收录该内容
4 篇文章 0 订阅
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  num1: integer = 5; //全局变量num1为每页要显示的记录条数 。
  num2: integer;

implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);//第一页
begin
  adoquery1.Connection := adoconnection1;
  adoquery1.SQL.clear;
  adoquery1.SQL.add('select top ' + inttostr(num1) + ' * from loginn');
  adoquery1.Open;
end;

procedure TForm1.Button2Click(Sender: TObject);//下一页
begin
  adoquery1.Connection := adoconnection1;
  adoquery1.SQL.clear;
  adoquery1.sql.add('select count(*) as countnum from loginn');
  adoquery1.Open;
  inc(num2, num1);
  if num2 >= adoquery1.FieldByName('countnum').AsInteger then
    num2 := trunc(adoquery1.FieldByName('countnum').AsInteger / 5) * 5;
  adoquery1.SQL.clear;
  adoquery1.SQL.Add('select top ' + inttostr(num1) + ' * from loginn where user not in(select top ' + inttostr(num2) + ' user from loginn)');
  adoquery1.Open;
end;

procedure TForm1.Button3Click(Sender: TObject);//上一页
begin
  dec(num2, num1);
  if num2 > 5 then
  begin
    adoquery1.Connection := adoconnection1;
    adoquery1.SQL.clear;
    adoquery1.SQL.Add('select top ' + inttostr(num1) + ' * from loginn where user not in(select top ' + inttostr(num2) + ' user from loginn)');
    adoquery1.open;
  end
  else
  begin
    inc(num2, num1);
    adoquery1.Connection := adoconnection1;
    adoquery1.sql.clear;
    adoquery1.SQL.Add('select top ' + inttostr(num1) + ' * from loginn');
    adoquery1.Open;
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);//最后一页
begin
  adoquery1.connection := adoconnection1;
  adoquery1.sql.clear;
  adoquery1.SQL.Add('select count(*) as countnum from loginn');
  adoquery1.Open;
  num2 := trunc(adoquery1.FieldByName('countnum').AsInteger / 5) * 5;
  adoquery1.SQL.Clear;
  adoquery1.SQL.Add('select top ' + inttostr(num1) + ' * from loginn where user not in(select top ' + inttostr(num2) + ' user from loginn)');
  adoquery1.Open;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  adoquery1.Connection := adoconnection1;
  adoquery1.SQL.clear;
  adoquery1.SQL.add('select top ' + inttostr(num1) + ' * from loginn');
  adoquery1.Open;
  datasource1.dataset := adoquery1;
  dbgrid1.DataSource := datasource1;
end;

end.

 

  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值