开发者

How to delete 2 items with same name at once from dataset?

开发者 https://www.devze.com 2023-01-18 08:03 出处:网络
i have a list in Twwgrid as below: i wan to delete the same stock at once with the right-click pop-up menu as shown above (eg. delete both OREO_CHOC_CREAM either is CTN or UNIT). But now i only can

i have a list in Twwgrid as below:

How to delete 2 items with same name at once from dataset?

i wan to delete the same stock at once with the right-click pop-up menu as shown above (eg. delete both OREO_CHOC_CREAM either is CTN or UNIT). But now i only can delete 1 by 1 with the code below, any idea to solve this problem?

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


I'm assuming the dataset is ordered by stock #, if not, it will not work.

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;

enjoy. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消