4

I have a TListView linked in LiveBindings with a TFDMemTable. I load data in the FDMemTable using LoadFromFile (I have for example 20 record).

When I delete a record from the FDMemTable, the RecordCount is decreased but the TListView is not refreshed, it continue to display the 20 records loaded on the LoadFormFile.

If, with the FDMemTable: I .SaveToFile, .Close, and reload with .LoadFromFile, the TListView now display the change.

This is the same behavior if I use the CachedUpdate of FDMemTable or not.

I've tried to call TFDMemTable.Refresh and TListView.Repaint without succes.

Is it possible to call the TListView to refresh his "linked" set of data ?

When I delete a record in the FDMemTable, why no visible refresh occur on the TListView ?

EDIT: I must add a thing, the record is deleted programmatically.

The desired functionality is to delete sone unwanted record in the FDMemTable and display the remaining record to the user with the TListView.

Alain V
  • 311
  • 3
  • 17

2 Answers2

3

In LiveBindings Designer, by linking the Synch of the ListView to the * of the FDMemTable, the ListView now display the resulting record.

LiveBindings Designer

Also in my last algorithm, because I use .BeginBatch / .EndBatch (that disable data-aware refreshing), while processing data for removing unwanted record I've to disable the LiveBinding link temporarily (cause I use different sorting index whitin the processing of the data): LinkListControlToField1.Active := false; and "re-link" it after processing: LinkListControlToField1.Active := true;

Alain V
  • 311
  • 3
  • 17
2

The livebindings isn't consistently bi-directional here. The ListView livebindings was designed to work from the UI to the dataset direction, but only mostly.

If you enable CanSwipeDelete, you can expect that to work, if you know how.

In my case, on Android, I found myself writing code to ensure that the listview kept sync with the dataset, even though there is livebindings active. In my case is a TClientDataset named CDSAnimals, with a unique key value of TagID. I hope this helps.

procedure TfrmLiveMain.ListView1DeletingItem(Sender: TObject; AIndex: Integer;
  var ACanDelete: Boolean);
var
  LI: TListViewItem;
  LIO: TListItemText;
begin

  // check that the livebindings is doing it's job, if not
  // do it myself
  ACanDelete := False;
  LI := ListView1.Items[AIndex];
  LIO := LI.Objects.FindObjectT<TListItemText>('Text1');
  FTagID := LIO.Text;
  if ClientModule2.CDSAnimals.FieldByName('TagID').AsString <> FTagID then
    ClientModule2.CDSAnimals.Locate('TagID', FTagID, []);
  if ClientModule2.CDSAnimals.FieldByName('TagID').AsString = FTagID then
  begin
    ACanDelete := True; // causes the listview item to be deleted without
                        // affecting the corresponding dataset record
  end;

end;

procedure TfrmLiveMain.ListView1DeleteItem(Sender: TObject; AIndex: Integer);
begin

  // this is called with the wrong index!
  if ClientModule2.CDSAnimals.Locate('TagID', FTagID, []) then
    if ClientModule2.CDSAnimals.FieldByName('TagID').AsString = FTagID then
      begin
        // now delete the corresponding record too
        ClientModule2.CDSAnimals.Delete; // and it works!
      end;

end;
Freddie Bell
  • 2,186
  • 24
  • 43
  • Your idea drive me to a possible workaroud: deleting the item object of the `TListView` WHILE deleting record in the `TFDMemTable`. I don't know if it's a bug in Delphi but I've tried with some TMS component and this problem do not occur. – Alain V Nov 10 '18 at 20:33
  • Another workaround is to work with 2 `TFDMemTable`, One for process the deleting of unwanted record and one for display (using LiveBindings) then usigng `CopyDataSet` to copy the process dataset in the display dataset. This is working (we must `.Close` the display dataset before unsing `CopyDataSet`). – Alain V Nov 10 '18 at 20:36
  • Did you ever encounter any of the errors mentioned [here](https://stackoverflow.com/questions/69791689/how-to-delete-a-database-record-linked-to-a-listview-item)? this happens after calling `delete` on the dataset record and LiveBinding is trying to delete the `listViwItem` that doesn't exist anymore. – codeGood Nov 27 '22 at 20:57