TreeView和ListView数据库查询数据联动操作
好久不用了,重新整理下放这里以备需要使用,功能见图
数据库表结构
定义TreeView addObject中data存储的记录集
type PNode = ^TNode; TNode = record id: Integer; tcmc: string; mxid: string; end;
填充TreeView代码
procedure TForm1.FillTree(TreeView: TTreeView); var findq: TADOQuery; node: TTreeNode; p: PNode; //这个方法是根据记录的id字段值,查找TreeView上的父节点 function FindParentNode(id: Integer): TTreeNode; var i: Integer; begin Result := nil; for i := 0 to TreeView.Items.Count - 1 do begin //比较Node的Data值和记录的id值 if Integer(PNode(TreeView.Items[i].Data).id) = id then begin Result := TreeView.Items[i]; Break; end; end; end; begin findq := TADOQuery.Create(nil); findq.ConnectionString := 'Provider=SQLOLEDB.1;Password=Sa123;Persist Security Info=True;User ID=demo;Initial Catalog=dd;Data Source=127.0.0.1'; try TreeView.Items.BeginUpdate; TreeView.Items.Clear; findq.SQL.Text := 'SELECT c.id,c.tcmc,c.pid,c.mxid FROM 处方套餐 AS c WITH(NOLOCK) ORDER BY c.id'; findq.Open; while not findq.Eof do begin //将数据放入结构体以便双击时使用 New(p); p.id := findq.FieldByName('id').AsInteger; p.tcmc := findq.FieldByName('tcmc').AsString; p.mxid := findq.FieldByName('mxid').AsString; //如果是最顶级,直接添加到TreeView if findq.FieldByName('pid').AsInteger = 0 then //将导航树保存在Node的Data中,以便查找用 TreeView.Items.AddObject(nil, findq.FieldByName('tcmc').AsString, TObject(p)) else begin //查找上级节点,存在上级节点添加子节点 node := FindParentNode(findq.FieldByName('pid').AsInteger); if node <> nil then TreeView.Items.AddChildObject(node, findq.FieldByName('tcmc').AsString, TObject(p)); end; findq.Next; end; finally findq.Free; TreeView.FullExpand; //展开树 TreeView.Items.EndUpdate; end; end;
查询数据调用代码
procedure TForm1.Button1Click(Sender: TObject); begin FillTree(TreeView1); end;
双击TreeView填充ListView
procedure TForm1.TreeView1DblClick(Sender: TObject); var mxid: string; str: TStrings; I: Integer; strSql: TStrings; QryTemp: TADOQuery; begin if not TreeView1.Selected.HasChildren then //当前选中是否含有子项,如果有则展开不执行操作 begin str := TStringList.Create; strSql := TStringList.Create; QryTemp := TADOQuery.Create(Self); try QryTemp.ConnectionString := 'Provider=SQLOLEDB.1;Password=Sa123;Persist Security Info=True;User ID=demo;Initial Catalog=dd;Data Source=127.0.0.1'; //mxid值:107,108,109,110,111,112 mxid := PNode(TreeView1.Items[TreeView1.Selected.AbsoluteIndex].Data).mxid; //根据分割符号添加mxid到StringList str.Delimiter := ','; str.DelimitedText := mxid; //查询数据 strSql.Clear; strSql.Add('SELECT a.DiagnosisProjectID, a.DiagnosisProjectName'); strSql.Add('FROM DDDiagnosisProject AS a WITH(NOLOCK) '); strSql.Add('WHERE a.DiagnosisProjectID=' + QuotedStr(str[0])); for I := 1 to str.Count - 1 do begin strSql.Add(' OR a.DiagnosisProjectID=' + QuotedStr(str[I])); end; strSql.Add('ORDER BY a.DiagnosisProjectID'); with QryTemp do begin Close; SQL.Text := strSql.Text; Open; end; //查询存在数据就添加数据到ListView if QryTemp.RecordCount > 0 then begin QryTemp.First; ListView1.Items.Clear; while not QryTemp.Eof do begin with ListView1.Items.Add do begin Caption := QryTemp.FieldByName('DiagnosisProjectID').AsString; SubItems.Add(QryTemp.FieldByName('DiagnosisProjectName').AsString); end; QryTemp.Next; end; end; finally str.Free; strSql.Free; QryTemp.Free; end; end; end;
点击ListView点击行选中CheckBox
procedure TForm1.ListView1Click(Sender: TObject); begin ListView1.Items[ListView1.Selected.Index].Checked := not ListView1.Items[ListView1.Selected.Index].Checked; end;
ListView全选
procedure TForm1.btnSelectAllClick(Sender: TObject); var I: Integer; begin for I := 0 to ListView1.Items.Count - 1 do begin ListView1.Items[I].Checked := True; end; end;
ListView取消选中
procedure TForm1.btnClearSelectClick(Sender: TObject); var I: Integer; begin for I := 0 to ListView1.Items.Count - 1 do begin ListView1.Items[I].Checked := False; end; end;
ListView反选
procedure TForm1.btnUnSelectClick(Sender: TObject); var I: Integer; begin for I := 0 to ListView1.Items.Count - 1 do begin ListView1.Items[I].Checked := not ListView1.Items[I].Checked; end; end;
查看ListView选中项
procedure TForm1.btnAllCheckedClick(Sender: TObject); var i: Integer; s: string; begin Memo1.Clear; for i := 0 to ListView1.Items.Count - 1 do begin if ListView1.Items[i].Checked then begin s := s + ListView1.Items[i].Caption + ':' + ListView1.Items[i].SubItems[0] + #13#10; end; end; Memo1.Text := s; end;
本文来自博客园,作者:liessay,转载请注明原文链接:https://www.cnblogs.com/liessay/p/14954142.html