笨一点,将源代码贴出来,大家帮我诊断诊断。。。
unit Pub;
interface
uses SysUtils, Types, Classes, ComCtrls, Graphics, Controls, ADODB;
type
TkqTreeView = class(TTreeView)
private
FOrganization: string;
FConn: TADOConnection;
FActive: Boolean;
FDeptTable: WideString;
FEmployeeTable: WideString;
FinCludeEmp:Boolean;
procedure AddTreeView(ParentNode: TTreeNode; DeptRs: _Recordset; Level: Integer);
procedure SetOrganization(Value: string);
procedure SetConn(Value: TADOConnection);
procedure SetActive(Value: Boolean);
procedure SetDeptTable(Value: WideString);
procedure SetEmployeeTable(Value: WideString);
procedure SetIncludeEmp(Value :Boolean);
protected
public
constructor Create(Aowner: TComponent); override;
destructor Destroy; override;
function AddTreeNode(ParentNode: TTreeNode; Text: string; xType: Integer): TTreeNode;
procedure RefreshTreeView;
published
property Organization: string read FOrganization write SetOrganization;
property Connection: TADOConnection read FConn write SetConn;
property Active: Boolean read FActive write SetActive;
property DeptTable: WideString read FDeptTable write SetDeptTable;
property EmployeeTable: WideString read FEmployeeTable write SetEmployeeTable;
property IncludeEmployee :Boolean read FincludeEmp write setIncludeEmp;
end;
procedure Register;
implementation
constructor TkqTreeView.Create(Aowner: TComponent);
begin
inherited Create(AOwner);
FActive := False;
FOrganization := '#########';
FDeptTable := 'Dept';
FEmployeeTable := 'PersonnelTable';
FincludeEmp := False;
end;
destructor TkqTreeView.Destroy;
begin
inherited Destroy;
end;
procedure Register;
begin
RegisterComponents('Click', [TkqTreeView]);
end;
procedure TkqTreeView.SetOrganization(Value: string);
begin
FOrganization := Value;
if Items.Count > 0 then
Items[0].Text := Value;
end;
procedure TkqTreeView.SetConn(Value: TADOConnection);
begin
FConn := Value;
if FActive then SetActive(False);
end;
procedure TkqTreeView.SetActive(Value: Boolean);
begin
FActive := Value;
if Value then RefreshTreeView
else Items.Clear;
end;
procedure TkqTreeView.SetDeptTable(Value: WideString);
begin
FDeptTable := Value;
if FActive then SetActive(False);
end;
procedure TkqTreeView.SetEmployeeTable(Value: WideString);
begin
FEmployeeTable := Value;
if FActive then SetActive(False);
end;
//向目录树添加一个树结点,类型根据xType决定
function TkqTreeView.AddTreeNode(ParentNode: TTreeNode; Text: string; xType: Integer): TTreeNode;
begin
Result := Items.AddChild(ParentNode, Text);
Result.ImageIndex := xType;
Result.SelectedIndex := xType;
Result.StateIndex := xType;
Result.Data := Pointer(xType);
end;
procedure TkqTreeView.AddTreeView(ParentNode: TTreeNode; DeptRs: _Recordset; Level: Integer);
var
Rs: _Recordset;
i, j: Integer;
Node: TTreeNode;
bh,Name: string;
SQL: string;
begin
for i := 0 to DeptRs.RecordCount - 1 do
begin
bh := DeptRs.Fields['ID'].Value;
Name :=DeptRs.Fields['BmName'].Value;
//增加一个新结点
Node := AddTreeNode(ParentNode, bh + ' ' + Name, 1);
SQL := 'SELECT * FROM ' + FDeptTable + ' WHERE bmLevel=' + IntToStr(Level+1) + ' AND ParentID="' + bh + '"';
// AddTreeView(Node, MainForm.ADOConn.Execute('exec _GetDeptBySub WITH "' + bh + '"'), Level+1);
AddTreeView(Node, FConn.Execute(SQL), Level+1);
If FincludeEmp then //是否包含人员信息
Begin
Rs := FConn.Execute('select * from ' + FEmployeeTable + ' where ssbm="' + bh + '"');
for j := 0 to Rs.RecordCount - 1 do
begin
//增加一个新结点
AddTreeNode(Node, Rs.Fields['bh'].Value + ' ' + Rs.Fields['xm'].Value, 2);
Rs.MoveNext;
end;
end;
DeptRs.MoveNext;
end;
end;
procedure TkqTreeView.RefreshTreeView;
begin
Items.Clear;
with Items.Add(nil, FOrganization) do
begin
Data := Pointer(0);
ImageIndex := 0;
SelectedIndex := 0;
StateIndex := 0;
end;
AddTreeView(Items.GetFirstNode, FConn.Execute('SELECT * FROM ' + FDeptTable + ' WHERE bmLevel=0'), 0);
FullExpand;
end;
procedure TkqTreeView.SetIncludeEmp(Value: Boolean);
begin
FincludeEmp := Value;
end;
end.