Home > Back-end >  Avoid blank Items in ListView Delphi 10.4.2
Avoid blank Items in ListView Delphi 10.4.2

Time:03-04

I'm trying to display data from TFDQuery to TListView, but I don't want to use a Data Binding.

When I fetch the data, I have a blank Item between every record. How can I avoid these blank items?

DM.Qdomains.Connection := DM.Conn;
DM.Qdomains.SQL.Text := 'SELECT * FROM domains';
DM.Qdomains.Active := True;
DM.Qdomains.First;
try
  while not DM.Qdomains.Eof do
  begin
    with DomainsListView.Items.Add do
    begin
      DomainsListView.Items.Add.Data['domain'] := DM.Qdomains.FieldByName('domain').AsString;
    end;
    DM.Qdomains.Next;
  end;
finally
  DM.Qdomains.Free;
end;

Blank Items

CodePudding user response:

You are calling TListView.Items.Add() twice per DB record:

with DomainsListView.Items.Add do // <-- 1st call
begin
  DomainsListView.Items.Add.Data['domain'] := ...; // <-- 2nd call
end;

Simply get rid of the redundant Add() call, eg:

DM.Qdomains.Connection := DM.Conn;
DM.Qdomains.SQL.Text := 'SELECT * FROM domains';
DM.Qdomains.Active := True;
DM.Qdomains.First;
try
  while not DM.Qdomains.Eof do
  begin
    DomainsListView.Items.Add.Data['domain'] := DM.Qdomains.FieldByName('domain').AsString;
    DM.Qdomains.Next;
  end;
finally
  DM.Qdomains.Free;
end;

CodePudding user response:

I assume you don't have this single text, so I think your ListView appearance is Dynamic ? I agree Remy's response, but I can't but ask why you don't use Livebindings to fill your ListView (so easy and no code) ?

(sorry to put this as an answer, but low reputation => no comment nor vote)

  • Related