Delphi中的Dbgrid删除一条记录 [英] Delphi Dbgrid delete a record

查看:552
本文介绍了Delphi中的Dbgrid删除一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个标签页。我的PageControl中的每个标签页包含一个带有一些按钮的Dbgrid来操作此dbgrid。



有时,当我点击按钮删除,我的DBGrid不会改变。删除后,我在我的DBgrid中看到我的值。

 如果MyTable.recordcount> 0 then 
begin
str:='Name of name字段/ **'+ MyTable.FieldByName('Name')。AsString +'** /';
如果Application.MessageBox(PChar(str),PChar('NAME'),MB_OKCANCEL + MB_ICONQUESTION)= IDOK然后
begin
MyTable.Delete;
end;
end;

我尝试添加

  MyTable.Refresh;删除后的

,但不起作用。
MessageBox中myfield的值是正确的!



当我在MyDBgrid中选择另一个Tabsheet时,会出现此问题。
在我的DBGrid中可能是焦点的问题?这可能是我的DBGrid(dsBrowser,dsEdit,...)的状态的问题?



========= ========================================== p>

我尝试为BeforeDelete事件和AfterDelete事件创建日志:

  Log(1,'------------------------------------ Before Del'); 
Log(1,Format('DataSet.IsEmpty:%s',[BoolToStr1(DataSet.IsEmpty)]));
Log(1,Format('DataSet.State:%d',[Ord(DataSet.State)]));
Log(1,'--------');
Log(1,Format('DataSet.RecNo:%d',[DataSet.RecNo]));
Log(1,Format('DataSet.TabSheet:%s',[DataSet.FieldByName('TabSheetName')。AsString]));
Log(1,Format('DataSet.FieldName:%s',[DataSet.FieldByName('FieldName')。AsString]));
Log(1,Format('DataSet.FieldId:%s',[DataSet.FieldByName('FieldId')。AsString])); AfterDelete事件中的

  Log(1,'------------------------------------ After Del '); 
Log(1,Format('DataSet.IsEmpty:%s',[BoolToStr1(DataSet.IsEmpty)]));
Log(1,Format('DataSet.State:%d',[Ord(DataSet.State)]));
Log(1,'--------');
Log(1,Format('DataSet.RecNo:%d',[DataSet.RecNo]));
Log(1,Format('DataSet.TabSheet:%s',[DataSet.FieldByName('TabSheetName')。AsString]));
Log(1,Format('DataSet.FieldName:%s',[DataSet.FieldByName('FieldName')。AsString]));
Log(1,Format('DataSet.FieldId:%s',[DataSet.FieldByName('FieldId')。AsString]));

我使用标签页:



在此输入图片说明



我获得此日志

  ---------------------- -------------- Before Del 
DataSet.IsEmpty:False
DataSet.State:1
--------
DataSet.RecNo:7
DataSet.TabSheet:tabsheet_0
DataSet.FieldName:
DataSet.FieldId:03
--------------- --------------------- Del
DataSet.IsEmpty:False
DataSet.State:1
----- ---
DataSet.RecNo:6
DataSet.TabSheet:tabsheet_0
DataSet.FieldName:
DataSet.FieldId:03

您可以在我的日志中看到:在删除包含<$ c的行后,数据集不会更改(文件名和fieldId不更改/ RecNo属性更改) $ c> 03 。

解决方案

由于您的问题是间歇性的,



首先要做的是设置一个日志功能并在数据集的 BeforeDelete 和 AfterDelete 事件。您应该记录当前数据集行的标识,数据集的状态( dsBrowse dsEdit 等)您认为可能相关的任何其他内容(例如活动标签页的标识),并查看您是否可以发现删除失败的情况。你可以将你的日志调用的结果写到表单上的TMemo。



Ime,只有在数据集在 dsBrowse state。



顺便说一下,我假设你知道如果数据集在 dsInsert dsSetKey state?从你所描述的,我会开始调试,调查是否在 dsInsert 状态,当你的问题出现。当然,你可以通过捕获它的 BeforeInsert 事件并调用你的日志来捕获数据集到 dsInsert 函数。


I have a multiple tabsheet. Each tabsheet in my PageControl that contain an Dbgrid with some button to manipulate this dbgrid.

Sometimes, when i click on button remove, my DBGrid not change. After delete, i see my value in my DBgrid.

If MyTable.recordcount > 0 then
begin
 str := 'Value of name field/**' + MyTable.FieldByName('Name').AsString+'**/';
 If Application.MessageBox(PChar(str), PChar('NAME'), MB_OKCANCEL + MB_ICONQUESTION) = IDOK then
 begin
      MyTable.Delete;
 end;
end;

I try to add

 MyTable.Refresh;

after delete but it doesn't work. The value of myfield in the MessageBox is correct!

This problem appear when i select another Tabsheet in MyDBgrid. It can be problem of focus in my DBGrid? It can be problem of state of my DBGrid(dsBrowser, dsEdit,...)?

=========================================================

I try to create a log for BeforeDelete event and AfterDelete event :

Log(1,'------------------------------------Before Del');
Log(1,Format('DataSet.IsEmpty:%s',[BoolToStr1(DataSet.IsEmpty)]));
Log(1,Format('DataSet.State:%d',[Ord(DataSet.State)]));
Log(1,'--------');
Log(1,Format('DataSet.RecNo:%d',[DataSet.RecNo]));
Log(1,Format('DataSet.TabSheet:%s',[DataSet.FieldByName('TabSheetName').AsString]));
Log(1,Format('DataSet.FieldName:%s',[DataSet.FieldByName('FieldName').AsString]));
Log(1,Format('DataSet.FieldId:%s',[DataSet.FieldByName('FieldId').AsString])); 

on AfterDelete event :

Log(1,'------------------------------------After Del');
Log(1,Format('DataSet.IsEmpty:%s',[BoolToStr1(DataSet.IsEmpty)]));
Log(1,Format('DataSet.State:%d',[Ord(DataSet.State)]));
Log(1,'--------');
Log(1,Format('DataSet.RecNo:%d',[DataSet.RecNo]));
Log(1,Format('DataSet.TabSheet:%s',[DataSet.FieldByName('TabSheetName').AsString]));
Log(1,Format('DataSet.FieldName:%s',[DataSet.FieldByName('FieldName').AsString]));
Log(1,Format('DataSet.FieldId:%s',[DataSet.FieldByName('FieldId').AsString])); 

I use the tabsheet as :

enter image description here

I obtain this log

------------------------------------Before Del
DataSet.IsEmpty:False
DataSet.State:1
--------
DataSet.RecNo:7
DataSet.TabSheet:tabsheet_0
DataSet.FieldName:
DataSet.FieldId:03
------------------------------------After Del
DataSet.IsEmpty:False
DataSet.State:1
--------
DataSet.RecNo:6
DataSet.TabSheet:tabsheet_0
DataSet.FieldName:
DataSet.FieldId:03

You can see in my log: the dataset not change (filedname and fieldId not change / the RecNo properties is change ) after delete the row that contain 03.

解决方案

Since your problem is intermittent, you'll have to debug it yourself, because readers can't do that for you.

The first thing to do is to set up a logging function and call it in the dataset's BeforeDelete and AfterDelete events. You should record in it the identity of the current dataset row, the dataset's state (dsBrowse, dsEdit, etc) and anything other you think might be relevant (e.g. the identity of the active tabsheet) and see if you can spot in what circumstances the Delete fails. You could write the results of your logging calls to a TMemo on your form.

Ime, it's good practice only to allow a Delete if the dataset is in dsBrowse state.

By the way, I assume you are aware that a Delete operation is automatically aborted if the dataset is in dsInsert or dsSetKey state? From what you've described, I'd start debugging by investigating whether it's in dsInsert state when your problem arises. Of course, you can catch when the dataset is being put into dsInsert state by catching its BeforeInsert event and calling your logging function from there.

这篇关于Delphi中的Dbgrid删除一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆