最近客户有个需求,要求对单据列表里指定的单据进行批量审核,很自然的,我想到了在DBGridEh增加一栏复选框的列,审核时遍历所有单据,将打了勾的单据审核就可以了。查阅了网上很多文章,不外有2个方案,1是在数据表里增加字段,2是直接在DBGridEh增加一个没有字段的栏,也许是我笨吧,2种方案试过了都不行,于是只能自己想办法了。研究了半天结合前面的2个方案,我想到了一个解决的方法,分享给大家吧。
一、在sql语句里增加一个字段
原来:select * from tbPerson
修改为:select *,bSel=Name from tbPerson
这样就多了一个和Name相同的字段,这个bSel就是一会要做成复选框栏目对应的字段
二、DBGridEh增加一列,字段名就是SQL语句里增加的bSel,CheckBoxes设为True, KeyList第1行是1,第2行是0。
三、在DataSet打开后,要将这个字段的内容清空,设为默认值0,为未选中状态。我用的是ADOQuery,设LockType为ltBatchOptimistic,允许修改表格内容。
procedure TForm_PRW0.QueryListAfterOpen(DataSet: TDataSet);
begin
with QueryList do
begin
First;
try
DisableControls; //禁止滚屏
while not Eof do
begin
if (FieldByName('bSel').AsString<>'1') and (FieldByName('bSel').AsString<>'0') then //设置默认值是0,就是未选中状态
begin
Edit;
FieldByName('bSel').AsString:='0';
Post;
end;
Next;
end;
First;
EnableControls; //允许滚屏
except
end;
end;
end;
四、在审核过程中使用这个字段
1、全选
procedure TForm_PRW0.ActionSelAllExecute(Sender: TObject);
var
vMark:TBookmark;
begin
with QueryList do
begin
if not Active then Exit;
vMark:=GetBookmark;
First;
try
DisableControls;
while not Eof do
begin
Edit;
FieldByName('bSel').AsString:='1';
Next;
end;
GotoBookmark(vMark);
except
end;
EnableControls;
end;
end;
2、审核前遍历所有单据
procedure TForm_PRW0.ActionGroupCheckExecute(Sender: TObject);
var
vMark:TBookmark;
vState,BillList:string;
bNoChange:Boolean;
begin
if not QueryList.Active then Exit;
if not ActionCheck.Visible then Exit;
vMark:=QueryList.GetBookmark;
vState:=QueryList.FieldByname('State').AsString;
if vState='' then
vState:=DupeString('0',GV_StateLength);
if MessageDlg('确认要批量审核所选的单据吗?',mtConfirmation,mbOKCancel,0)<>mrOK then
Exit;
bNoChange:=False;PKList:='';BillList:='';
with QueryList do
begin
First;
while not Eof do
begin
vState:=FieldByName('state').AsString;
if (Copy(vState,1,1)<>'1') or (FieldByName('bSel').AsString<>'1') then //车间未确认或未选中 不操作
begin
bNoChange:=True;
Next;
Continue;
end;
PKList:=PKList+ Format(',''%s''',[FieldByName('PKID').AsString]);
BillList:=BillList+ Format(',%s',[FieldByName('BillNo').AsString]);
Next;
end;
if PKList='' then
begin
MessageDlg('没有发现要审核的单据!',mtInformation,[mbOK],0);
Exit;
end else
begin
PKList:=Copy(PKList,2,Length(PKList));
BillList:=Copy(BillList,2,Length(BillList));
end;
end;
end;
---------------------