如何从数据集中一次删除2个具有相同名称的项目?

时间:2010-09-30 03:28:00

标签: delphi

我在Twwgrid中有一个列表如下: alt text

我想用右键单击弹出菜单一次删除相同的库存,如上所示(例如,删除OREO_CHOC_CREAM或者是CTN或UNIT)。但现在我只能用下面的代码逐一删除,有什么想法解决这个问题吗?

procedure Tfrm1.mniDeleteClick(Sender: TObject);
begin
  inherited;
    with grdItems.DataSource.DataSet do begin
      if (RecordCount <> 0) and (MessageBox( Application.Handle, 'Delete Record ?', 'Confirmation', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2 + MB_APPLMODAL) = IDYES) then
         Delete;
    end;
end;

1 个答案:

答案 0 :(得分:3)

我假设数据集是按库存#排序的,如果没有,它将无效。

procedure Tfrm1.DeleteByStockNum();
var
  StockN: string;
  DataSet: TDataSet;
begin
  DataSet := grdItems.DataSource.DataSet;
  DataSet.DisableControls;
  try
    StockN := DataSet.Fields[0].AsString;
    //locating the very first record with this stock #
    while (not DataSet.BOF) and
          (DataSet.Fields[0].AsString = StockN) do
      DataSet.Previous;
    if DataSet.Fields[0].AsString <> StockN then
      //we are one record above
      DataSet.Next;
    //lets delete all the matching records
    while (not DataSet.IsEmpty)
           and (DataSet.Fields[0].AsString = StockN) do
      DataSet.Delete;
  finally
    DataSet.EnableControls;
  end;
end;


procedure Tfrm1.mniDeleteClick(Sender: TObject);
begin
  inherited;
    with grdItems.DataSource.DataSet do begin
      if (RecordCount <> 0) and (MessageBox( Application.Handle, 'Delete Record ?', 'Confirmation', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2 + MB_APPLMODAL) = IDYES) then
         DeleteByStockNum;
    end;
end;

享受。 :)