大问题:dbgrid 与 clientdataset 的 filter 过滤导致记录移动的问题。(100分)

  • 主题发起人 主题发起人 yanghaijun
  • 开始时间 开始时间
Y

yanghaijun

Unregistered / Unconfirmed
GUEST, unregistred user!
用 clientdataset 连接数据集后,设置其 filter,然后修改 dbgrid 的某记录,当换一种过滤方式后,再切换回原来的 filter,修改的记录总移动到了 dbgrid 的最后面,这是怎么回事,如何解决?就是说要保证在切换回来后记录的位置是不变的<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;SocketConnection1: TSocketConnection;<br> &nbsp; &nbsp;ClientDataSet1: TClientDataSet;<br> &nbsp; &nbsp;DataSource1: TDataSource;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;DBGrid1: TDBGrid;<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure ClientDataSet1FilterRecord(DataSet: TDataSet;<br> &nbsp; &nbsp; &nbsp;var Accept: Boolean);<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp; &nbsp;v: string;<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;clientdataset1.Open<br>end;<br><br>procedure TForm1.ClientDataSet1FilterRecord(DataSet: TDataSet;<br> &nbsp;var Accept: Boolean);<br>begin<br> &nbsp;accept:=dataset.fieldbyname('编号').asstring=v;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp; clientdataset1.Filtered:=false;<br> &nbsp; if v='19' then v:='20' else v:='19';<br> &nbsp; clientdataset1.Filtered:=true;<br>end;<br>比如我点了 button1 做了一次过滤,修改了第一个记录,然后我再点 button1 则使用另一个参数过滤,第三次再点,结果在 dbgrid1 中刚才修改过的记录跑到了最后一行了,这显然不是我想要的。<br>到底是 dbgrid 的问题还是 clientdataset 的问题?
 
建立在主键上的排序,然后过虑应该就不会出现你说的问题了。(当然不能修改主键字段)
 
使用TBookMark与DisableControls
 
同意WickedladII,使用BookMark标记。
 
如何使用 bookmark 以避免记录在 dbgrid 中的移动?
 
to Johnny_du: 请举例
 
OK了,谢谢 Johnny_du
 
var<br> &nbsp;p:TBookMark;<br>begin<br> &nbsp;with ClientDataSet1 do<br> &nbsp;begin<br> &nbsp; &nbsp;p:=GetBookmark;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;.........<br> &nbsp; &nbsp; &nbsp;GotoBookMark(p);<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;FreeBookMark(p);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
后退
顶部