刷新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.
- //THackDBGrid = class(TDBGrid)
- //refresh datagrid data - preserve row position
- 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;
Note the protected hack used here(THackDBGrid) to get the hidden (protected) Row property!
刷新dbgrid 而不失去当前行位置的更多相关文章
- Python3 Selenium WebDriver网页的前进、后退、刷新、最大化、获取窗口位置、设置窗口大小、获取页面title、获取网页源码、获取Url等基本操作
Python3 Selenium WebDriver网页的前进.后退.刷新.最大化.获取窗口位置.设置窗口大小.获取页面title.获取网页源码.获取Url等基本操作 通过selenium webdr ...
- Selenium WebDriver-网页的前进、后退、刷新、最大化、获取窗口位置、设置窗口大小、获取页面title、获取网页源码、获取Url等基本操作
通过selenium webdriver操作网页前进.后退.刷新.最大化.获取窗口位置.设置窗口大小.获取页面title.获取网页源码.获取Url等基本操作 from selenium import ...
- 解决DBGridEh遍历记录后不移动当前行位置的方法
解决DBGridEh遍历记录后不移动当前行位置的方法 在用DBGridEh配合ClientDataSet使用时,需要知道用户选择了哪些记录,可用遍历记录的方法查询选择列是否为真,但在这之后,Clien ...
- ASPX的Timer位置没放正确,导致整页刷新,而不是UpdatePanel里的内容刷新。
提示:Timer应该放在UpdatePanel的ContentTemplate标签里,才行.放在外面的话,会导致整页刷新.
- JQuery实现页面刷新滚动条自动滚动到特定位置
var cotentOffset = $('#6f').offset(); $('.info_box').animate({ scrollLeft: cotentOffset.left }, ); 原 ...
- ASP.Net页面刷新后自动滚动到原来位置
在网上搜索之后总结了三种方式: 1.设置Page中的MaintainScrollPositionOnPostback属性为true A>.页面里有MaintainScrollPositionOn ...
- Sticky Footer (让页脚永远停靠在页面底部,而不是根据绝对位置)
<!doctype html><html> <head> <meta charset="UTF-8"> <meta name= ...
- Asp.net 在刷新或提交页面后保持滚动条的位置
网页内容在较长时,每次回传刷新页面或提交网页时都会定位到最顶端,非常不利于用户交互. 将Page.MaintainScrollPositionOnPostBack属性值设置为true即可实现刷新后保持 ...
- JS刷新页面后滚动条的位置不变
有时候,在网页中点击了页面中的按钮或是刷新了页面后,页面滚动条又 会回到顶部,想看后面的记录就又要拖动滚动条,或者要按翻页键,非常不方便,想在提交页面或者在页面刷新的时候仍然保持滚动条的位置不变,最好 ...
随机推荐
- 编译、安装rdesktop 1.8.3
来自:https://blog.csdn.net/songshimvp1/article/details/48290885 1.安装GCC: 安装C/C++编译器apt-get install gcc ...
- ubuntu 图形化界面 gui 桌面版 root登录 sorry,that didn't work.please try again! 抱歉,认证失败。请重试
出现这种问题,用下面的方法就行了 https://jingyan.baidu.com/article/bad08e1e224b2709c85121f1.html 而且我发现,因为我用的是英文版的ubu ...
- Performance Monitor2:性能计数器
性能计数器(Performance Counter)是量化系统状态或活动的一个数值,Windows Performance Monitor在一定时间间隔内(默认的取样间隔是15s)获取Performa ...
- ITerm常用的快捷键
新建标签:command + t 关闭标签:command + w 切换标签:command + 数字 或者 command + 左右方向键 切换全屏:command + enter 查找:comma ...
- yum 出现error: db5 error
yum 安装k8s的过程中用了 Ctrl+ z, 然后yum 再也不能使用了: Error: rpmdb open failed 解决方法: rpm --rebuilddb yum clean all ...
- Iron Speed Designer设计工具开发总结
9.0版本: 1.1 ISP和VS不要同时生成,代码写在override方法之下,不然生成之后会覆盖;正常情况下,ISP可以写代码,只不过没有快捷提示,一般我们先注释一下字段(如://sdsfdsfd ...
- PHP.ini 能不能加载子配置文件 ?
答案是不能,php这个地方用的是另一个方案解决的 编译的时候 用这个参数 --with-config-file-scan-dir指定一个目录 然后在这个目录里面加载ini https://www. ...
- export命令详解
基础命令学习目录首页 export 的基本作用就是将父shell中的局部变量设置为环境变量,使得该变量可以在子shell中使用.下面设置两种情景对export进行原理解析. 情景 1. 有一个名为m ...
- 哪些场景下无法获得上一页referrer信息
哪些场景下无法获得上一页referrer信息 直接在浏览器地址栏中输入地址: 使用 location.reload() 刷新(location.href 或者 location.replace() ...
- It isn't possible to write into a document from an asynchronously-loaded
It isn't possible to write into a document from an asynchronously-loaded 今天遇到了一个问题: 通过document.wri ...