delphi简单的分页实现(高手可以不看)
适合初学者,练习几个控件的使用
1、先放一张界面控件布局:
4个button,2个label,1个edit,1个TRzsplitter(把界面分割成上下2部分),1一个adoquery,1个adoconnection,1个datdasource,1个DBgridEh(原生的dbgrid也可以)
2、双击窗体,编写连接数据库的代码:
1 procedure TForm3.FormCreate(Sender: TObject); 2 begin 3 ADOConnection1.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=数据库用户名;Initial Catalog=数据库名字;PassWord=数据库密码;Data Source=数据库服务器IP'; 4 ADOConnection1.Connected := True; 5 adoquery1.Connection := adoconnection1; 6 adoquery1.SQL.clear; 7 adoquery1.SQL.add('select top ' + Edit1.Text + ' * from TableName order by id'); 8 adoquery1.Open; 9 datasource1.dataset := adoquery1; 10 dbgrid1.DataSource := datasource1; 11 end;
注意:adoconnection控件的LoginPrompt的属性值改为False(为什么是False自己可以去试一下)
3、双击 首页 按钮,编写代码:
1 //第一页 2 procedure TForm3.btnFirstPageClick(Sender: TObject); 3 begin 4 adoquery1.SQL.clear; 5 adoquery1.SQL.add('select top ' + Edit1.Text + ' * from TableName order by id'); 6 adoquery1.Open; 7 num2 := 0; 8 end;
4、双击 上一页 按钮,编写代码:
//上一页 procedure TForm3.btnPreviousPageClick(Sender: TObject); begin dec(num2, StrToInt(Edit1.Text)); if num2 >= StrToInt(Edit1.Text) then begin adoquery1.SQL.clear; adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName where id not in(select top ' + inttostr(num2) + ' id from TableName order by id) order by id'); adoquery1.open; end else begin inc(num2, StrToInt(Edit1.Text)); adoquery1.sql.clear; adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName order by id'); adoquery1.Open; end; end;
5、编写下一页 按钮 编写代码:
//下一页 procedure TForm3.btnNextPageClick(Sender: TObject); begin adoquery1.SQL.clear; adoquery1.sql.add('select count(*) as countnum from TableName'); adoquery1.Open; inc(num2, StrToInt(Edit1.Text)); if num2 >= adoquery1.FieldByName('countnum').AsInteger then begin num2 := trunc(adoquery1.FieldByName('countnum').AsInteger / StrToInt(Edit1.Text)) * StrToInt(Edit1.Text); end; adoquery1.SQL.clear; adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName where id not in(select top ' + inttostr(num2) + ' id from TableName order by id) order by id'); adoquery1.Open; if DBGrid1.DataSource.DataSet.RecordCount <= 0 then begin adoquery1.SQL.clear; adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName where id not in(select top ' + inttostr(num2-StrToInt(Edit1.Text)) + ' id from TableName order by id) order by id'); adoquery1.Open; end; end;
6、双击 最后一页 按钮,编写代码:
//最后一页 procedure TForm3.btnLastPageClick(Sender: TObject); var val1 :Integer; val2 :Integer; begin adoquery1.sql.clear; adoquery1.SQL.Add('select count(*) as countnum from TableName'); adoquery1.Open; val1 := (adoquery1.FieldByName('countnum').AsInteger) mod StrToInt(Edit1.Text); val2 := (adoquery1.FieldByName('countnum').AsInteger) - StrToInt(Edit1.Text); adoquery1.SQL.Clear; num2 := val2; if val1 = 0then begin adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName where id not in(select top ' + IntToStr(val2) + ' id from TableName order by id) order by id'); end else begin adoquery1.SQL.Add('select top ' + IntToStr(val1) + ' * from TableName where id not in(select top ' + IntToStr(val2) + ' id from TableName order by id) order by id'); end; adoquery1.Open; end;
7、完整代码:
1 unit Unit3; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids, RzDBGrid, ExtCtrls, RzPanel, RzSplit, DBGridEhGrouping, GridsEh, 8 DBGridEh; 9 10 type 11 TForm3 = class(TForm) 12 btnFirstPage: TButton; 13 btnNextPage: TButton; 14 btnPreviousPage: TButton; 15 btnLastPage: TButton; 16 ADOQuery1: TADOQuery; 17 ADOConnection1: TADOConnection; 18 DataSource1: TDataSource; 19 Edit1: TEdit; 20 lblCount1: TLabel; 21 lblCount2: TLabel; 22 RzSplitter1: TRzSplitter; 23 DBGrid1: TDBGridEh; 24 procedure btnFirstPageClick(Sender: TObject); 25 procedure FormCreate(Sender: TObject); 26 procedure btnNextPageClick(Sender: TObject); 27 procedure btnPreviousPageClick(Sender: TObject); 28 procedure btnLastPageClick(Sender: TObject); 29 private 30 { Private declarations } 31 public 32 { Public declarations } 33 end; 34 35 var 36 Form3: TForm3; 37 num2: integer; 38 39 implementation 40 41 {$R *.dfm} 42 43 //第一页 44 procedure TForm3.btnFirstPageClick(Sender: TObject); 45 begin 46 adoquery1.SQL.clear; 47 adoquery1.SQL.add('select top ' + Edit1.Text + ' * from TableName order by id'); 48 adoquery1.Open; 49 num2 := 0; 50 end; 51 52 //最后一页 53 procedure TForm3.btnLastPageClick(Sender: TObject); 54 var 55 val1 :Integer; 56 val2 :Integer; 57 begin 58 adoquery1.sql.clear; 59 adoquery1.SQL.Add('select count(*) as countnum from TableName'); 60 adoquery1.Open; 61 val1 := (adoquery1.FieldByName('countnum').AsInteger) mod StrToInt(Edit1.Text); 62 val2 := (adoquery1.FieldByName('countnum').AsInteger) - StrToInt(Edit1.Text); 63 adoquery1.SQL.Clear; 64 num2 := val2; 65 if val1 = 0then 66 begin 67 adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName where id not in(select top ' + IntToStr(val2) + ' id from TableName order by id) order by id'); 68 end 69 else 70 begin 71 adoquery1.SQL.Add('select top ' + IntToStr(val1) + ' * from TableName where id not in(select top ' + IntToStr(val2) + ' id from TableName order by id) order by id'); 72 end; 73 adoquery1.Open; 74 end; 75 76 //下一页 77 procedure TForm3.btnNextPageClick(Sender: TObject); 78 begin 79 adoquery1.SQL.clear; 80 adoquery1.sql.add('select count(*) as countnum from TableName'); 81 adoquery1.Open; 82 inc(num2, StrToInt(Edit1.Text)); 83 if num2 >= adoquery1.FieldByName('countnum').AsInteger then 84 begin 85 num2 := trunc(adoquery1.FieldByName('countnum').AsInteger / StrToInt(Edit1.Text)) * StrToInt(Edit1.Text); 86 end; 87 adoquery1.SQL.clear; 88 adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName where id not in(select top ' + inttostr(num2) + ' id from TableName order by id) order by id'); 89 adoquery1.Open; 90 if DBGrid1.DataSource.DataSet.RecordCount <= 0 then 91 begin 92 adoquery1.SQL.clear; 93 adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName where id not in(select top ' + inttostr(num2-StrToInt(Edit1.Text)) + ' id from TableName order by id) order by id'); 94 adoquery1.Open; 95 end; 96 end; 97 98 //上一页 99 procedure TForm3.btnPreviousPageClick(Sender: TObject); 100 begin 101 dec(num2, StrToInt(Edit1.Text)); 102 if num2 >= StrToInt(Edit1.Text) then 103 begin 104 adoquery1.SQL.clear; 105 adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName where id not in(select top ' + inttostr(num2) + ' id from TableName order by id) order by id'); 106 adoquery1.open; 107 end 108 else 109 begin 110 inc(num2, StrToInt(Edit1.Text)); 111 adoquery1.sql.clear; 112 adoquery1.SQL.Add('select top ' + Edit1.Text + ' * from TableName order by id'); 113 adoquery1.Open; 114 end; 115 end; 116 117 procedure TForm3.FormCreate(Sender: TObject); 118 begin 119 ADOConnection1.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=数据库用户名;Initial Catalog=数据库名字;PassWord=数据库密码;Data Source=数据库服务器IP'; 120 ADOConnection1.Connected := True; 121 adoquery1.Connection := adoconnection1; 122 adoquery1.SQL.clear; 123 adoquery1.SQL.add('select top ' + Edit1.Text + ' * from TableName order by id'); 124 adoquery1.Open; 125 datasource1.dataset := adoquery1; 126 dbgrid1.DataSource := datasource1; 127 end; 128 129 end.