delphi中如何正确使用Listview?

发布于 2024-12-22 18:17:47 字数 777 浏览 4 评论 0原文

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

_________________________________
全名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;

但我想要类似的东西:

在此处输入图像描述

My code is the below, it's working correctly but, but after compiling program i see all the fullname and country listed vertically something like :

_________________________________
Fullname1
Country1
Fullname2
Country2
Fullname3
Country3
etc...

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;

But i want something Like :

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦亦如幻 2024-12-29 18:17:47

首先:添加列标题:

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;

然后添加记录,如下所示:

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;

更新:

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

First: add the column headers:

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;

then add the records as follows:

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;

Update:

Of course, in order to get the list as in your screenshot, you need to set the ListView's ViewStyle property to vsReport

烂人 2024-12-29 18:17:47

您的代码应该如下所示:

var
  ListItem: TListItem;

  ...

  ListView.Items.BeginUpdate;
  try
    while(not SQLQuery1.EOF)do begin
      ListItem:= ListView.Items.Add;
      ListItem.Caption:= 'Full name: '+SQLQuery1['fullname'];
      with ListItem.SubItems do begin
        Add('Country: '+SQLQuery1['cntry']);
// if you need more columns, add here
      end;
      SQLQuery1.Next;
    end;
  finally
    ListView.Items.EndUpdate;
  end;

您还应该将 ListView.Style 设置为 vsReport 以将 listview 显示为网格。

Your code should look like that:

var
  ListItem: TListItem;

  ...

  ListView.Items.BeginUpdate;
  try
    while(not SQLQuery1.EOF)do begin
      ListItem:= ListView.Items.Add;
      ListItem.Caption:= 'Full name: '+SQLQuery1['fullname'];
      with ListItem.SubItems do begin
        Add('Country: '+SQLQuery1['cntry']);
// if you need more columns, add here
      end;
      SQLQuery1.Next;
    end;
  finally
    ListView.Items.EndUpdate;
  end;

You should also set ListView.Style to vsReport to show listview as grid.

若相惜即相离 2024-12-29 18:17:47

我不确定如何将列表视图设置为多行,但我确实知道您没有正确使用查询。
就目前情况而言,您的代码存在 SQL 注入漏洞,并且循环内对“fieldbyname”的隐式引用使其速度变慢。

var
  FullName: TField;
  Country: TField;
  ListItem: TListItem;
begin
  //Use Params or suffer SQL-injections
  SQLQuery1.SQL.Text := 'SELECT * FROM users where user_age= :age';
  SQLQuery1.ParamByName('age').AsInteger:= age;
  SQLQuery1.Open;
  if SQLQuery1.RecordCount = 0 then Exit;
  //Never use `FieldByName` inside a loop, it's slow.
  FullName:= SQLQuery1.FieldByName('fullname');
  Country:= SQLQuery1.FieldByName('cntry');
  ListView1.Style:= vsReport;

  SQLQuery1.First; // move to the first record
  SQLQuery1.DisableControls; //Disable UI updating until where done.
  try
    ListView1.Items.BeginUpdate;
    //ListView1.Visible := false;
    while (not SQLQuery1.EOF) do begin
      //Code borrowed from @Serg
      ListItem:= ListView.Items.Add;
      ListItem.Caption:= 'Full name: '+Fullname.AsString;
      ListItem.SubItems.Add('Country: '+Country.AsString);
      SQLQuery1.Next;  
    end; {while}
  finally
    SQLQuery1.EnableControls;
    ListView1.Items.EndUpdate;
  end;
end;

I'm not sure how to get the listview to multiline, but I do know you're not using the Query correctly.
As it stands your code has an SQL-injection hole and the implicit reference to 'fieldbyname' inside the loop makes it slow.

var
  FullName: TField;
  Country: TField;
  ListItem: TListItem;
begin
  //Use Params or suffer SQL-injections
  SQLQuery1.SQL.Text := 'SELECT * FROM users where user_age= :age';
  SQLQuery1.ParamByName('age').AsInteger:= age;
  SQLQuery1.Open;
  if SQLQuery1.RecordCount = 0 then Exit;
  //Never use `FieldByName` inside a loop, it's slow.
  FullName:= SQLQuery1.FieldByName('fullname');
  Country:= SQLQuery1.FieldByName('cntry');
  ListView1.Style:= vsReport;

  SQLQuery1.First; // move to the first record
  SQLQuery1.DisableControls; //Disable UI updating until where done.
  try
    ListView1.Items.BeginUpdate;
    //ListView1.Visible := false;
    while (not SQLQuery1.EOF) do begin
      //Code borrowed from @Serg
      ListItem:= ListView.Items.Add;
      ListItem.Caption:= 'Full name: '+Fullname.AsString;
      ListItem.SubItems.Add('Country: '+Country.AsString);
      SQLQuery1.Next;  
    end; {while}
  finally
    SQLQuery1.EnableControls;
    ListView1.Items.EndUpdate;
  end;
end;
独孤求败 2024-12-29 18:17:47

Delphi 文档包含这个示例,它完全符合您的要求。

procedure TForm1.FormCreate(Sender: TObject);
const
  Names: array[0..5, 0..1] of string = (
    ('Rubble', 'Barney'),
    ('Michael', 'Johnson'),
    ('Bunny', 'Bugs'),
    ('Silver', 'HiHo'),
    ('Simpson', 'Bart'),
    ('Squirrel', 'Rocky')
    );

var
  I: Integer;
  NewColumn: TListColumn;
  ListItem: TListItem;
  ListView: TListView;
begin
  ListView := TListView.Create(Self);
  with ListView do
  begin
    Parent := Self;
    Align := alClient;
    ViewStyle := vsReport;

    NewColumn := Columns.Add;
    NewColumn.Caption := 'Last';
    NewColumn := Columns.Add;
    NewColumn.Caption := 'First';

    for I := Low(Names) to High(Names) do
    begin
      ListItem := Items.Add;
      ListItem.Caption := Names[I][0];
      ListItem.SubItems.Add(Names[I][2]);
    end;
  end;
end;

尽管 Delphi 文档饱受诟病,但它经常有像这样非常有用的示例。示例的网关页面位于此处,这些示例甚至可以在 sourceforge 这样你就可以使用你最喜欢的 svn 客户端查看它们。

The Delphi documentation contains this example that does exactly what you want.

procedure TForm1.FormCreate(Sender: TObject);
const
  Names: array[0..5, 0..1] of string = (
    ('Rubble', 'Barney'),
    ('Michael', 'Johnson'),
    ('Bunny', 'Bugs'),
    ('Silver', 'HiHo'),
    ('Simpson', 'Bart'),
    ('Squirrel', 'Rocky')
    );

var
  I: Integer;
  NewColumn: TListColumn;
  ListItem: TListItem;
  ListView: TListView;
begin
  ListView := TListView.Create(Self);
  with ListView do
  begin
    Parent := Self;
    Align := alClient;
    ViewStyle := vsReport;

    NewColumn := Columns.Add;
    NewColumn.Caption := 'Last';
    NewColumn := Columns.Add;
    NewColumn.Caption := 'First';

    for I := Low(Names) to High(Names) do
    begin
      ListItem := Items.Add;
      ListItem.Caption := Names[I][0];
      ListItem.SubItems.Add(Names[I][2]);
    end;
  end;
end;

For all that the Delphi documentation is much maligned, it often has very useful examples like this. The gateway page to the examples is here and the examples are even available on sourceforge so you can check them out using your favourite svn client.

小矜持 2024-12-29 18:17:47
Procedure TForm1.GetUsers;
var
  ListItem: TListItem;
begin
  try
    ListView1.Items.BeginUpdate;
    try
      ListView1.Clear;
      MySQLQuery.SQL.Clear;
      MySQLQuery.SQL.Add('select * from users;');
      MySQLQuery.Open;
      while (not MySQLQuery.EOF) do
      begin
        ListItem := ListView1.Items.Add;
        ListItem.Caption:= VarToSTr(MySQLQuery['username']);
        with ListItem.SubItems do
          begin
            Add(VarToSTr(MySQLQuery['password']));
            Add(VarToSTr(MySQLQuery['maxscore']));
          end;
        MySQLQuery.Next;
      end;
      MySQLQuery.Close;
    finally
      ListView1.Items.EndUpdate;
    end;
  except
    on E: Exception do
        MessageDlg(PWideChar(E.Message), TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
  end;
end;
Procedure TForm1.GetUsers;
var
  ListItem: TListItem;
begin
  try
    ListView1.Items.BeginUpdate;
    try
      ListView1.Clear;
      MySQLQuery.SQL.Clear;
      MySQLQuery.SQL.Add('select * from users;');
      MySQLQuery.Open;
      while (not MySQLQuery.EOF) do
      begin
        ListItem := ListView1.Items.Add;
        ListItem.Caption:= VarToSTr(MySQLQuery['username']);
        with ListItem.SubItems do
          begin
            Add(VarToSTr(MySQLQuery['password']));
            Add(VarToSTr(MySQLQuery['maxscore']));
          end;
        MySQLQuery.Next;
      end;
      MySQLQuery.Close;
    finally
      ListView1.Items.EndUpdate;
    end;
  except
    on E: Exception do
        MessageDlg(PWideChar(E.Message), TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文