glen morse
glen morse

Reputation: 73

Delete row from DBGrid only

I have a DBGrid that is updated on a button click. Once updated I have it check each row vs an XML file. If the fields in that row match the data in the XML file, I want to delete that row from the dbgrid only.

The DBgrid is loaded from ADOconnection / Query / Datasource . The main connection is to a DB which is linked to an excel file. The XML file is just a report we get each month. Normally we have to check to make sure all the machines on the excel file is also on the report given to us.. With hundreds of machines I am trying to auto detect any difference. but I cant figure out how to delete the row in the grid only when it finds a match Thus only the non-Matched items are in the grid.

Upvotes: 1

Views: 1780

Answers (3)

Ken White
Ken White

Reputation: 125767

You can't delete rows from a DBGrid. You delete them from the dataset to which the DBGrid is connected. A DBGrid simply displays the data from a dataset. If you want the row deleted, delete it from that dataset using it's .Delete method.

After reading the edit you made, it looks to me like you should be loading the query data into a TClientDataSet (CDS), and then attaching that CDS to the DBGrid's DataSource. You can then do whatever you want to that CDS without affecting the original data, as long as you don't call the CDS's ApplyUpdates method.

Upvotes: 5

Brian
Brian

Reputation: 7299

One option to make rows/records vanish from a dbGrid and other data aware controls is to use the TDataSet's OnFilterRecord and return false for rows you no longer want seen. It does not have any effect on the underlying data - it just filters out what is visible.

procedure TDM1.FDQueryTopicListFilterRecord(DataSet: TDataSet; var Accept: Boolean);
begin
   // Check if the row should be shown - set Accept to true if it should, false if not
end;

Upvotes: 2

MartynA
MartynA

Reputation: 30755

What Ken White says in his answer is 100% correct, but I think there is something else maybe worth pointing out, especially as you mention XML.

Unlike some other TDataSet descendants, both TClientDataSet and the more modern FireDAC datasets implement the UpdateStatus property of the dataset and this reflects what has happened to the current record in the dataset - see the Online Help for details. It is an enumeration [1] declared as

type
  TUpdateStatus = (usUnmodified, usModified, usInserted, usDeleted);

You say you want to compare the records with what you have in an XML file and delete the records which have not been updated "from the DBGrid", which as Ken has said is not meaningful per se.

However there is another way of going about what you seem to want. If you can load the data displayed in the dataset feeding the grid from the XML file. then you know that until you update the dataset, the records must match the XML. So, once the records have been updated in your app, rather than comparing all the records in the dataset with their XML counterparts, you could simply delete (from the dataset) the records whose UpdateStatus is usUnmodified. This is trivial to do using a method like this:

procedure TForm1.DeleteUnmodifiedRecords;
begin
  ClientDataSet1.DisableControls;
  try
    ClientDataSet1.First;
    while not ClientDataSet1.Eof do begin
      if ClientDataSet1.UpdateStatus = usUnModified then
        ClientDataSet1.Delete
      else
      ClientDataSet1.Next;
      end;
  finally
    ClientDataSet1.EnableControls;
  end;
end;

and this will, of course, remove the records from the DBGrid.

Of course, this assumes that you can load the XML data into a TClientDataSet or a FireDAC dataset, eithe directly or by transforming the XML into a form that either of these can load using a TXmlTransform component but I won't go into the details of that here.

[1]: A point to beware of is that usModified does not seem to be the exact logical negation of usUnmodified. This has the slightly surprising consequence that if you apply a StatusFilter of [usModified] to a CDS, records that have been changed can appear twice, once in their original form and once in their changed form - see fig 6.5 of Cary Jensen's "Delphi in Depth: ClientDataSets", 1st edition.

Upvotes: 1

Related Questions