DbGrid和数据集控制问题(100分)

  • 主题发起人 主题发起人 青云
  • 开始时间 开始时间

青云

Unregistered / Unconfirmed
GUEST, unregistred user!
我在控制DbGrid 和数据集数据一致的时候遇到了些麻烦,这个问题我考虑了很久,一直苦思不得其解。我想一定有不少朋友也遇到类似问题,希望大家能探讨一下:


操作步骤:
1、把DbGrid里的编号值 001 修改成 002;
2、点击按钮;
现象:
数据集里的值没有没有修改成002
原因:
DbGrid上的格子没有改变焦点,所以数据集的数据发生变化

希望实现的功能:
在点击 按钮前加一个函数,让数据集在未提交(post)时和DbGrid上显示的值保持一致



提出人: 青云 sy_dzc@yahoo.com.cn


程序代码下载:http://bbs.2ccc.com/attachments/2006/qingyun_200622016922.rar
 
在網格的onexit事件提交,
Automatically posts or cancels data changes when the active record changes.

Delphi syntax:

procedure CheckBrowseMode;

C++ syntax:

void __fastcall CheckBrowseMode(void);

Description

CheckBrowseMode is used internally by many dataset methods to ensure that modifications to the active record are posted when a dataset? state is dsEdit, dsInsert, or dsSetKey and a method switches to a different record.

If State is dsEdit or dsInsert, CheckBrowseMode calls UpdateRecord, and, if the Modified property for the dataset is true, calls Post. If Modified is false, CheckBrowseMode calls Cancel.
If State is dsSetKey, CheckBrowseMode calls Post.
If State is dsInactive, CheckBrowseMode raises an exception.

If an application uses existing dataset methods, CheckBrowseMode is always called when necessary, so there is usually no need to call CheckBrowseMode directly.

Applications that provide custom dataset routines may need to call CheckBrowseMode inside those routines to guarantee that changes are posted when switching to a different record.
 
楼上的朋友,为什么我提供的程序里用的是ToolButton,而不是TButton,
因为ToolButton不能获取焦点,我做这个例子的目的就是为了实现:
在DbGrid没有办法失去焦点的时候,如何处理;

如果有这个函数:DbGrid1.LostFouces; 那样也行啊;
可是现在只有 DbGrid1.SetFocus 啊;
 
hsgrass朋友,非常对不起;

我使用了你的 procedure CheckBrowseMode;
确实解决了部分问题。

但是:

procedure TDataSet.CheckBrowseMode;
begin
CheckActive;
DataEvent(deCheckBrowseMode, 0);
case State of
dsEdit, dsInsert:
begin
UpdateRecord;
if Modified then Post else Cancel;
end;
dsSetKey:
Post;
end;
end;

这个函数强迫post 了,如果只是判断,
我尝试了一下:
用:
if qry.State<>dsbrowse then
qry.UpdateRecord;
可以不提交(post)数据
 
随便说个不太优雅的方法:在你的ToolButton事件响应函数里让Form获取焦点,或者其它的任何一个可以获得焦点的控件。这样GRID不就失去焦点了么。
 
你可以在BUTTON的CLICE事件中执行数据集的POST前加一个判断条件呀
 if (activecontrol is dbgrid) then

else
post
 
多谢楼上各位朋友的无私帮助。
 
后退
顶部