关于Tlistview的问题,很急(100分)

  • 主题发起人 主题发起人 kk2000
  • 开始时间 开始时间
K

kk2000

Unregistered / Unconfirmed
GUEST, unregistred user!
我在onshow 的事件中加入了以下语句(小弟很少用此控件望各位能详细说明):
var
subitm:Tlistitem;
I,J:integer;
begin
dm.ADOQypjbtj1.Connection:=dm.ADOCnn;(这是ADOQUERY连接的属性)
listview1.Checkboxes:=true;(使用Tlistview的选择框)
//把数据加入到Tlistview
dm.ADOQypjbtj1.Close;
dm.ADOQypjbtj1.SQL.Clear;
dm.ADOQypjbtj1.SQL.Add('select * from ypjbfljlb1');
dm.ADOQypjbtj1.Open;
listview1.Items.Clear;
dm.ADOQypjbtj1.First;
while not dm.ADOQypjbtj1.Eof do
begin
subitm:=listview1.Items.add;
subitm.Caption:=dm.adoqypjbtj1['JBID'];
subitm.SubItems.append(dm.ADOQypjbtj1['JBLX']);
dm.ADOQypjbtj1.Next;
end;
dm.ADOQypjbtj1.Close;
end;
再加入以下语句:
procedure TForm10.ListView1Click(Sender: TObject);
var
str:string;
I,N,J,K:integer;
begin

dm.ADOQyjxz.Connection:=dm.ADOCnn;
dm.ADOQcount.Connection:=dm.ADOCnn;//用作计数器

I:=listview1.Selected.Index;//获取TLISTVIEW对象的相应行数

if listview1.Items.Item.Checked=false then
begin
//获取计数值
listview1.Items.Item.Checked:=true;//当某一行被选择时,同时CHECKBOX也被选择
dm.ADOQcount.Close;
dm.ADOQyjxz.SQL.Clear;
dm.ADOQcount.SQL.Add('select * from counter');
dm.ADOQcount.SQL.Add('where ID=''Y''');
dm.ADOQcount.Open;
N:=dm.ADOQcount['num'];
inc(N);
//添加所选择的记录
dm.ADOQyjxz.Close;
dm.ADOQyjxz.SQL.Clear;
dm.ADOQyjxz.SQL.Add('select * from yjxzjgjlb');
dm.ADOQyjxz.Open;
dm.ADOQyjxz.Append;
dm.ADOQyjxz['ID']:=N;
dm.ADOQyjxz['JBID']:=listview1.Items.Item.Caption;
dm.ADOQyjxz['JBLX']:=listview1.Items.Item.SubItems.Strings[0];
dm.ADOQyjxz.Post;
//计数器值加一保存
dm.ADOQcount.Edit;
dm.ADOQcount['num']:=N;
dm.ADOQcount.Post;
dm.ADOQcount.Close;
//在STRINGGRID显示选择结果
stringgrid1.Cells[1,N]:=inttostr(dm.ADOQyjxz['ID']);
stringgrid1.Cells[2,N]:=dm.ADOQyjxz['JBLX'];
dm.ADOQyjxz.Close;
exit;//退出此模块
end;
//

if listview1.Items.Item.Checked=true then
begin
listview1.Items.Item.Checked:=false;
str:=listview1.Items.Item.Caption;
dm.ADOQyjxz.Close;
dm.ADOQyjxz.SQL.Clear;
dm.ADOQyjxz.SQL.Add('select * from yjxzjgjlb');
dm.ADOQyjxz.SQL.Add('where JBID='''+str+'''');
dm.ADOQyjxz.Open;

//删除所选择的记录
if not dm.ADOQyjxz.IsEmpty then
begin
//取得所删除的序号
J:=dm.ADOQyjxz['ID'];
dm.ADOQyjxz.Delete;
//计数器减一
dm.ADOQcount.Close;
dm.ADOQcount.SQL.Clear;
dm.ADOQcount.SQL.Add('select * from counter');
dm.ADOQcount.SQL.Add('where ID=''Y''');
dm.ADOQcount.Open;
K:=dm.ADOQcount['num'];
dm.ADOQcount.Edit;
dm.ADOQcount['num']:=K-1;
dm.ADOQcount.Post;
dm.ADOQcount.Close;
end;//第二个if
// 进行序号修改
dm.ADOQyjxz.Close;
dm.ADOQyjxz.SQL.Clear;
dm.ADOQyjxz.SQL.Add('select * from yjxzjgjlb');
dm.ADOQyjxz.SQL.Add(' where ID >:J');
dm.ADOQyjxz.Parameters[0].Value:=J;
dm.ADOQyjxz.Open;

if not dm.ADOQyjxz.IsEmpty then
begin
dm.ADOQyjxz.First;

while not dm.ADOQyjxz.Eof do
begin
dm.ADOQyjxz.Edit;
dm.ADOQyjxz['ID']:=J;
dm.ADOQyjxz.Post;
stringgrid1.Cells[1,j]:=inttostr(dm.ADOQyjxz['ID']);
stringgrid1.Cells[2,j]:=dm.ADOQyjxz['JBLX'];
inc(j);
dm.ADOQyjxz.Next;
end;//while
stringgrid1.Cells[1,J]:='';
stringgrid1.Cells[2,J]:='';
dm.ADOQyjxz.Close;
end //第三个if
else //如果数据为空
begin
stringgrid1.Cells[1,J]:='';
stringgrid1.Cells[2,J]:='';
dm.ADOQyjxz.Close;
end;
exit;
end;//第一个if
end;
end.
但是在Tlistview里面没有数据的地方(也就是没显示的数据行)点击鼠标时
就会出错,有数据的地方就不会错了我真搞不懂(以上代码是没问题的我敢包证)
只是我对这个控件用得不熟,
请各位高手指点100分。OK



 
procedure TForm10.ListView1Click(Sender: TObject);
var
str:string;
I,N,J,K:integer;
ListItem: TListItem;
begin
ListItem := ListView1.Selected;
if ListItem = nil then Exit;
dm.ADOQyjxz.Connection:=dm.ADOCnn;
dm.ADOQcount.Connection:=dm.ADOCnn;//用作计数器

I:=listview1.Selected.Index;//获取TLISTVIEW对象的相应行数

if listview1.Items.Item.Checked=false then
begin
//获取计数值
listview1.Items.Item.Checked:=true;//当某一行被选择时,同时CHECKBOX也被选择
 
代码太长,我也不乐意看,既然你包票了,我就省了,

根据你的问题,加一句话就可以了
if ListView1.Selected = nil then
Exit;
 
多谢两位高手指点,小弟已经解决啦.
以后有问题还请两位多多帮忙
 
后退
顶部