zoukankan      html  css  js  c++  java
  • 刷新dbgrid 而不失去当前行位置

    我们有一个Delphi的数据库应用程序,上面有个DBGrid和一个数据集:
     

    DBGrid是用来显示来自数据集(查询或表)的数据,根据设计,当您调用已经打开的数据集的Refresh方

    法(例如使用DBNavigator的Refresh),当前行的位置将被设置为0 (第一个记录)。

    这意味着,如果用户选择了DBGrid的底部某个地方的一行记录,在刷新后,当前激活的行将改为第一行:

    (


    如果你一直在问:“有什么办法重新查询或刷新后,让TDBGrid中的数据留在准确位置(不改变位置)?

    ”这里的一个答案的问题:

    刷新DBGrid的数据 - 保留行的位置
    这里的一个小程序控制刷新DBGrid的内容后不会失去该行的位置。


     //THackDBGrid = class(TDBGrid)
     
     //refresh datagrid data - preserve rowposition
     procedure Refresh_PreservePosition;
     var
       rowDelta: Integer;
       row: integer;
       recNo: integer;
       ds : TDataSet;
     begin
       ds :=THackDBGrid(DBGrid1).DataSource.DataSet;
     
       rowDelta := -1 +THackDBGrid(DBGrid1).Row;
       row := ds.RecNo;
     
       ds.Refresh;
     
       with ds do
       begin
        DisableControls;
        RecNo := row;
        MoveBy(-rowDelta) ;
        MoveBy(rowDelta) ;
        EnableControls;
       end;
     end;


     请注意这里使用的保护破解(THackDBGrid)来获得隐藏的(protected)Row属性!

    英文原文

    There's DBGrid, there's adataset and we have a data awareDelphi application :)

    When DBGrid is used to display data from a dataset (query ortable), by design, after you call Refresh method on a dataset(re-open) (for example, using a DBNavigator), the current row position will be setto zero (first record).

    This means that if a user has selected a row somewhere near thebottom of a DBGrid, after a Refresh, the active row will be changedto first row :(

    If you have been asking "Is there any way to reopen or refresh aquery, leaving the TDBGrid data exactly where it is (withoutchanging the positions)?" Here's one answer to the problem:

    Refresh DBGrid Data - Preserve Row Position

    Here's a handly little procedure to refresh the content of aDBGrid without losing the row position.

    1.  
      //THackDBGrid = class(TDBGrid)
    2.  
       
    3.  
      //refresh datagrid data - preserve row position
    4.  
      procedure Refresh_PreservePosition;
    5.  
      var
    6.  
        rowDelta: Integer;
    7.  
        row: integer;
    8.  
        recNo: integer;
    9.  
        ds : TDataSet;
    10.  
      begin
    11.  
        ds := THackDBGrid(DBGrid1).DataSource.DataSet;
    12.  
       
    13.  
        rowDelta := -1 + THackDBGrid(DBGrid1).Row;
    14.  
        row := ds.RecNo;
    15.  
       
    16.  
        ds.Refresh;
    17.  
       
    18.  
        with ds do
    19.  
        begin
    20.  
          DisableControls;
    21.  
          RecNo := row;
    22.  
          MoveBy(-rowDelta) ;
    23.  
          MoveBy(rowDelta) ;
    24.  
          EnableControls;
    25.  
        end;
    26.  
      end;
    27.  
       

    Note the protected hack used here(THackDBGrid) to get the hidden (protected) Row property!

  • 相关阅读:
    网络协议栈(6)RFC793TCP连接时部分异常流程及实现
    网络协议栈(5)sendto/send返回成功意味着什么
    LeetCode——Detect Capital
    LeetCode——Find All Numbers Disappeared in an Array
    LeetCode——Single Number
    LeetCode——Max Consecutive Ones
    LeetCode——Nim Game
    LeetCode——Reverse String
    LeetCode——Next Greater Element I
    LeetCode——Fizz Buzz
  • 原文地址:https://www.cnblogs.com/jijm123/p/10367877.html
Copyright © 2011-2022 走看看