如何在delphi中正确使用Listview?

Raf*_*ari 1 delphi tlistview delphi-2010

我的代码如下,它工作正常但是,但是在编译程序后,我看到所有全名和国家/地区垂直列出的内容如下:

_________________________________全
名1
国家1全
名2
国家2全

3 国家3
等...

SQLQuery1.SQL.Text := 'SELECT * FROM users where user_age="'+age+'"';
SQLQuery1.Open;
rec := SQLQuery1.RecordCount;

SQLQuery1.First; // move to the first record
ListView1.Visible := false;
if rec>0 then
begin
while(not SQLQuery1.EOF)do begin
ListView1.Visible := true;
        // do something with the current item
ListView1.AddItem('Full name: '+SQLQuery1['fullname'], Self);
ListView1.AddItem('Country: '+SQLQuery1['cntry'], Self);

    // move to the next record

SQLQuery1.Next;

end;
Run Code Online (Sandbox Code Playgroud)

但我想要的东西像:

在此输入图像描述

Mar*_*ema 18

首先:添加列标题:

var
  Col: TListColumn;
begin
  Col := ListView1.Columns.Add;
  Col.Caption := 'Name';
  Col.Alignment := taLeftJustify;
  Col.Width := 140;

  Col := ListView1.Columns.Add;
  Col.Caption := 'Country';
  Col.Alignment := taLeftJustify;
  Col.Width := 140;
end;
Run Code Online (Sandbox Code Playgroud)

然后添加记录如下:

var
  Itm: TListItem;
begin
    // start of your query loop
    Itm := ListView1.Items.Add;
    Itm.Caption := SQLQuery1['fullname'];
    Itm.SubItems.Add(SQLQuery1['cntry']);
    // end of your query loop
end;
Run Code Online (Sandbox Code Playgroud)

更新:

当然,为了获得截图中的列表,您需要将ListView的ViewStyle属性设置为vsReport