delphi中如何正确使用Listview?
我的代码如下,它工作正常,但是,在编译程序后,我看到所有全名和国家/地区垂直列出,例如:
_________________________________
全名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 :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先:添加列标题:
然后添加记录,如下所示:
更新:
当然,为了获得屏幕截图中的列表,您需要将 ListView 的
ViewStyle
属性设置为vsReport
First: add the column headers:
then add the records as follows:
Update:
Of course, in order to get the list as in your screenshot, you need to set the ListView's
ViewStyle
property tovsReport
您的代码应该如下所示:
您还应该将
ListView.Style
设置为vsReport
以将 listview 显示为网格。Your code should look like that:
You should also set
ListView.Style
tovsReport
to show listview as grid.我不确定如何将列表视图设置为多行,但我确实知道您没有正确使用查询。
就目前情况而言,您的代码存在 SQL 注入漏洞,并且循环内对“fieldbyname”的隐式引用使其速度变慢。
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.
Delphi 文档包含这个示例,它完全符合您的要求。
尽管 Delphi 文档饱受诟病,但它经常有像这样非常有用的示例。示例的网关页面位于此处,这些示例甚至可以在 sourceforge 这样你就可以使用你最喜欢的 svn 客户端查看它们。
The Delphi documentation contains this example that does exactly what you want.
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.