tags:

views:

80

answers:

1

i have a list in Twwgrid as below: alt text

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, 'Delete Record ?', 'Confirmation', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2 + MB_APPLMODAL) = IDYES) then
         Delete;
    end;
end;
+2  A: 

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. :)

jachguate
it's really a big help. Thanks a lot! :)