使用原生ADO对数据进行分页显示<br>下面的页面中有Demo下载:<br>http://www.2ccc.com/article.asp?articleid=3520<br>源码如下:<br>unit MainFrm;<br><br>interface<br><br>uses<br> Classes, Controls, Forms, Grids, ToolWin, ComCtrls, StdCtrls, ExtCtrls, DB,<br> ADODB;<br><br>type<br> TfrmMain = class(TForm)<br> cnn: TADOConnection;<br> rst: TADODataSet;<br> pnlNavigate: TPanel;<br> btnFirst: TButton;<br> btnPrior: TButton;<br> btnNext: TButton;<br> btnLast: TButton;<br> edtPageNO: TEdit;<br> sgData: TStringGrid;<br> procedure FormCreate(Sender: TObject);<br> procedure btnFirstClick(Sender: TObject);<br> procedure edtPageNOKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);<br> private<br> FPageNo: Integer;<br> procedure SetPageNo(Value: Integer);<br> public<br> property PageNo: Integer read FPageNo write SetPageNo;<br> end;<br><br>var<br> frmMain: TfrmMain;<br><br>implementation<br><br>uses<br> Windows, SysUtils;<br><br>{$R *.dfm}<br><br>const<br> PAGE_SIZE = 6;<br><br>procedure TfrmMain.FormCreate(Sender: TObject);<br>var<br> i: Integer;<br>begin<br> SetWindowLong(edtPageNO.Handle, GWL_STYLE, GetWindowLong(edtPageNo.Handle,<br> GWL_STYLE) or ES_CENTER or ES_NUMBER);<br> rst.Active := True;<br> rst.Recordset.PageSize := PAGE_SIZE;<br> sgData.ColCount := rst.FieldCount + 1;<br> for i := 0 to rst.FieldCount - 1 do<br> begin<br> sgData.ColWidths[i + 1] := rst.Fields.DisplayWidth * Canvas.TextWidth('0');<br> sgData.Cells[i + 1, 0] := rst.Fields.DisplayName;<br> end;<br> sgData.Cells[0, 0] := '序号';<br> PageNo := 1;<br>end;<br><br>procedure TfrmMain.btnFirstClick(Sender: TObject);<br>begin<br> if Sender = btnFirst then<br> PageNo := 1<br> else if Sender = btnPrior then<br> PageNo := PageNo - 1<br> else if Sender = btnNext then<br> PageNo := PageNo + 1<br> else PageNo := rst.Recordset.PageCount;<br>end;<br><br>procedure TfrmMain.edtPageNOKeyDown(Sender: TObject; var Key: Word;<br> Shift: TShiftState);<br>begin<br> if edtPageNO.Text <> '' then<br> PageNo := StrToInt(edtPageNO.Text);<br>end;<br><br>procedure TfrmMain.SetPageNo(Value: Integer);<br>const<br> adStateOpen = $00000001;<br>var<br> n, i, j: Integer;<br>begin<br> with rst.Recordset do<br> if (FPageNo <> Value) and (Value >= 1) and (Value <= PageCount) and<br> (State = adStateOpen) then<br> begin<br> FPageNo := Value;<br> AbsolutePage := FPageNo;<br> n := (FPageNo - 1) * PAGE_SIZE;<br> edtPageNo.Text := IntToStr(FPageNo);<br> for i := 1 to PAGE_SIZE do<br> begin<br> sgData.RowCount := i + 1;<br> sgData.Cells[0, i] := IntToStr(n + i);<br> for j := 0 to Fields.Count - 1 do<br> sgData.Cells[j + 1, i] := Fields[j].Value;<br> MoveNext;<br> if Eof then Exit;<br> end;<br> end;<br>end;<br><br>end.