Dbgrid每页最多显示10条记录,按“→”键向下翻页,按“←”向上翻页。

解决方案 »

  1.   

    1、keydown事件:sendmessage(dbgrid.handle,WM_VSCROLL,dbgrid.datasource.dataset.recno+/-10,0)
    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;
      

  2.   

    procedure TForm1.dbgrd1KeyPress(Sender: TObject; var Key: Char);
    begin
      if Key=#37 then
        dbgrd1.Perform(WM_VSCROLL,SB_PAGEUP,0);
      if Key=#39
        dbgrd1.Perform(WM_VSCROLL,SB_PAGEDOWN,0);
    end;
      

  3.   

    Dbgrid每页最多显示10条记录 ,不知道上面几位怎么控制?
      

  4.   

    看來很多人喜歡現成的代碼。給一個思路,多嘗試一下,或許你可以發現更多東西。比直接copy and paste強多了
      

  5.   

    基本思路可以这样:前提:表TB1建立序列id先找出表TB1中最大,最小id:
      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;
      

  6.   


    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樓的分頁查詢呢?還是你的數據已經一次性取下來了,只是要翻頁而已?
      

  7.   

    那可以考慮這么做,不一定要一個ID,下面是我的一個demo,拿去改改。至于什么時候觸發這個方法,應該知道吧?procedure TForm1.FetchNextPage(ParamValue: string);
    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;