delphi 中出现dataset not in edit or insert mode的问题

self.ADOQuery2.Edit;
self.ADOQuery2.First;
while not self.ADOQuery2.Eof do
begin
self.ADOQuery2.FieldByName('单价').Value:=roundto(self.ADOQuery2.FieldByName('单价').Value,-2);
self.ADOQuery2.Next;
end;
self.ADOQuery2.Post;
这是显示在一个DBGrid里面
我是想把单价保留2位小数,
但是老出现这个问题,显示出错的self.ADOQuery2.Next句
我在网上也找了,都说是没进入edit状态,
但是我明明有edit,问题出在哪里,请帮帮忙
当你使用First或Next时ADOQuery2会自动Post,ADOQuery2处于dsBrowse状态(数据集已打开,可以浏览数据,但是不能进行修改)
所以你必须在赋值语句前使用Edit,我帮你修改程序如下:

self.ADOQuery2.First;
while not self.ADOQuery2.Eof do
begin
self.ADOQuery2.Edit;
self.ADOQuery2.FieldByName('单价').Value:=roundto(self.ADOQuery2.FieldByName('单价').Value,-2);
self.ADOQuery2.Post;
self.ADOQuery2.Next;
end;
//self.ADOQuery2.Edit; X
self.ADOQuery2.First;
while not self.ADOQuery2.Eof do
begin
ADOQuery2.Edit; ///
self.ADOQuery2.FieldByName('单价').Value:=roundto(self.ADOQuery2.FieldByName('单价').Value,-2);
ADOQuery2.Post; ///
self.ADOQuery2.Next;
end;
//self.ADOQuery2.Post; X

Edit针对当前游标所在行,你next的时候,游标移动了,这个时候会自动POST
 
 
原文地址:https://www.cnblogs.com/jijm123/p/10612496.html