请用LISTVIEW显示数据库查询结果

J198728 2009-09-19 10:57:42
做个单机版的就行,查询ACCESS里的数据,控件RADIOBUTTON() 用两个分别显示按姓名查询,按分厂查询,一个EDIT()控件 里面输入查询的内容,一个BUTTON()控件,点击它时在LISTVIEW里显示查询的结果,比如点击按姓名查询,输入姓名张三 然后点击BUUTTON按扭,LISTVIEW里显示张三的全部信息.最后再一个静态文本STATICTEXT()控件用来显示查询到数据的条数:
数据表的简单例子如下:
姓名 分厂 工号
张三 一厂 11
李四 二厂 22
王五 三厂 33

兄弟做了几次,结果是查询到的数据条数正确,但是LISTVIEW里的数据是一块白板,没有数据显示,LISTVIEW里的设置是没问题的.请大虾们贴出代码并简要说明,我的分全给了.我邮箱是jianghua0208@126.com,也可把DPR的文件发给我,拜托了!
...全文
496 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
J198728 2009-09-20
  • 打赏
  • 举报
回复
嗯,是的啊,用的ACCESS啊,我在网上下了一个实例,就是参照他的做的,但是他的能行,我仿照他的做的,怎么就不行了,您要我确定的几点都是没问题的.
Galer 2009-09-20
  • 打赏
  • 举报
回复
首先,这一句:
Sql.text := 'Select * from 2009';
from后面紧跟的是表名,你这里的2009是数据库表名称吗?
其次:
fieldbyName('姓名').asstring;
你用的是什么数据库管理系统,字段名称是中文的吗?

最后您确定您的adoquery连接到数据库了吗?
先确认这几点。
还有就是如果您的表明确实是2009,表字段名称是中文的,那么我建议您还是将表名和字段名称最好改为有意义的英文,至于为什么我想单从字符和中文的存储空间大小就能说明为什么。
飞天揽月 2009-09-19
  • 打赏
  • 举报
回复
只能遍历后显示 没有直接能够把数据倒过来的。

楼上的设置是正确的 Style一定要设置成表格形式 VSREPORT
Galer 2009-09-19
  • 打赏
  • 举报
回复
对了还有几十需要正确设置listview的viewstyle为vsreport,gridlines设为true,这样才能显示类似dbgrid的效果
Galer 2009-09-19
  • 打赏
  • 举报
回复
没点击一次button2,listview就增加一行,你可以转换成while循环将你的数据集记录加入到listview中,字段名成可以参照formcreate中的写法
Galer 2009-09-19
  • 打赏
  • 举报
回复
procedure TForm1.Button2Click(Sender: TObject);
//var
//tempV:TRect;
var
lst:TListItem;
begin


lst:=ListView1.Items.Add();

lst.Caption:='kkk'; //写第一列
lst.SubItems.Add('aaa'); //写第二列
lst.SubItems.Add('ss');
lst.SubItems.Add('bb'); //写第三列
lst.SubItems.Add('cc'); //写第四列


end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.Columns.Add();
listview1.Column[0].Caption:='11';
ListView1.Columns.Add();
ListView1.Columns.Add();
end;
J198728 2009-09-19
  • 打赏
  • 举报
回复
首先谢谢各位热心且耐心的回答:但我照做之后每步都照做,但是仍没数据显示出来.编译没有问题,我把代码贴出来,大家帮我找找错
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, ExtCtrls, DBCtrls, DB, ADODB, ComCtrls, Grids,
DBGrids;

type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
N1: TMenuItem;
PrintSetup1: TMenuItem;
Print1: TMenuItem;
N2: TMenuItem;
SaveAs1: TMenuItem;
Save1: TMenuItem;
Open1: TMenuItem;
New1: TMenuItem;
Edit1: TMenuItem;
Object1: TMenuItem;
Links1: TMenuItem;
N3: TMenuItem;
GoTo1: TMenuItem;
Replace1: TMenuItem;
Find1: TMenuItem;
N4: TMenuItem;
PasteSpecial1: TMenuItem;
Paste1: TMenuItem;
Copy1: TMenuItem;
Cut1: TMenuItem;
N5: TMenuItem;
Repeatcommand1: TMenuItem;
Undo1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
HowtoUseHelp1: TMenuItem;
SearchforHelpOn1: TMenuItem;
Contents1: TMenuItem;
Window2: TMenuItem;
Show2: TMenuItem;
Hide2: TMenuItem;
N12: TMenuItem;
ArrangeAll2: TMenuItem;
Cascade2: TMenuItem;
ile2: TMenuItem;
NewWindow2: TMenuItem;
Help2: TMenuItem;
About2: TMenuItem;
HowtoUseHelp2: TMenuItem;
utorial1: TMenuItem;
SearchforHelpOn2: TMenuItem;
Keyboard1: TMenuItem;
Procedures1: TMenuItem;
Commands1: TMenuItem;
Index1: TMenuItem;
Contents2: TMenuItem;
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
N6: TMenuItem;
neverhadadreamcometrue1: TMenuItem;
mylove1: TMenuItem;
N7: TMenuItem;
ListView1: TListView;
ADOQuery1: TADOQuery;
StaticText1: TStaticText;
Button1: TButton;
Label1: TLabel;
Edit2: TEdit;
procedure RadioButton1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
listview1.clear;
with adoquery1 do
begin
Sql.text := 'Select * from 2009';
Open ;

while not eof do
begin
with listview1.items.add do
begin
caption := fieldbyName('姓名').asstring;
subitems.add(fieldbyName('姓别').asstring);
subitems.add(fieldbyName('手机号').asstring);
subitems.add(fieldbyname('所在分厂').asstring);
subitems.Add(fieldbyname('手机号').asstring);
subitems.Add(fieldbyname('qq号').asstring);
end;
Next;
end;

end;
end;


procedure Tform1.Button1Click(Sender: TObject);
var
Content: string;
begin
try
ListView1.Items.Clear;

with ADOQuery1 do
begin
Content:=Edit2.Text;
SQL.Clear;
if RadioButton1.Checked then
begin
SQL.Add('select * from 2009 where 姓名 like :a');
Parameters.ParamByName('a').value := '%'+Content+'%';
end;
if RadioButton2.Checked then
begin
SQL.Add('select * from 2009 where 所在分厂 like :a');
Parameters.ParamByName('a').value := '%'+Content+'%';
end;
if RadioButton3.Checked then
begin
SQL.Add('select * from 2009 where 寝室号 like :a');
Parameters.ParamByName('a').value := Content;
end;
open ;
while not Eof do
begin
with ListView1.Items.Add do
begin
Caption := FieldByName('姓名').AsString;
subitems.add(fieldbyname('姓别').asstring);
SubItems.Add(FieldByName('手机号').AsString);
SubItems.Add(FieldByName('所在分厂').AsString);
SubItems.Add(FieldByName('寝室号').AsString);
SubItems.Add(FieldByName('qq号').AsString);

end;
Next;
end;

StaticText1.Caption:= '共'+IntToStr(RecordCount)+'条记录';
Close;
end;
except
MessageDlg('查询失败',mtError,[mbok],0);
end;
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
Edit2.Text := '';
end;



end
bdmh 2009-09-19
  • 打赏
  • 举报
回复
楼主首先要弄清楚listview不是DB组件,它不能像dbgrid那样直接连接数据库,数据集取到后,你需要循环数据集,逐条的将信息添加到listview中,才能看到结果

2,498

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧