先在你的查询的QUERY(叫QUERY1)里增加一个计算字段(fkCalculated)叫做sn
private
bklist:tlist;
procedure addtolist;
procedure Query1aGetText(Sender: TField; var Text: String;DisplayText: Boolean);
function BSearch(dataset:TDataset;sbmark: TBookmark; bmarkList: tlist): integer;
procedure TForm1.addtolist;
begin
bklist:=tlist.Create;
query1.first;
query1.DisableControls;
while not query1.eof do
begin
bklist.Add(query1.getbookmark);
query1.next;
end;
query1.first;
query1.EnableControls;
query1.FieldByName('sn').OnGetText:=query1aGetText;
end;
procedure TForm1.Query1aGetText(Sender: TField;
var Text: String; DisplayText: Boolean);
var
ab:TBookMark;
begin
if not query1.IsEmpty then
if bklist.Count<>0 then
begin
ab:=query1.getbookmark;
text:=inttostr(BSearch(query1,ab,bklist)+1);
query1.freebookmark(ab);
end;
end;
function TForm1.BSearch(dataset:TDataset;sbmark: TBookmark; bmarkList: tlist): integer;
var
sIndex,eIndex,midIndex,ipos:integer;
begin
try
result:=dataset.comparebookmarks(sbmark,bmarkList[0]);
if result=0 then Exit
else result:=-1;
except
end;
sIndex:=0;
eIndex:=bmarkList.Count-1;
while (result= -1) and (sindex<>eindex) do
begin
midIndex:=(sIndex+EIndex+1) div 2;
ipos:=dataset.comparebookmarks(sbmark,bmarkList[midIndex]);
if ipos=0 then
result:=midIndex
else
if ipos<0 then
eIndex:=midIndex
else sIndex:=midIndex;
end;
end;
然后只要你每次查询后调用addtolist 就可以把你的记录序号增加进去了。
如果DBGRID有字段,也要增加进去了。最后在关闭窗体时释放BKLIST