首页  编辑  

数据集处理成可视树

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:

(*//

标题:数据集处理成可视树

说明:处理父节点标识字段、节点文本字段、节点标识字段这种表结构

设计:Zswang

日期:2002-05-20

支持:wjhu111@21cn.com

//*)

///////Begin Source

function DataSetToTreeNode(mDataSet: TDataSet;

 mFieldNameParent: string; //父节点标识字段名

 mFieldNameTreeText: string; //节点文本字段名

 mFieldNameTreeId: string; //节点标识字段名

 mTreeView: TTreeView; mTreeNode: TTreeNode;

 mParentText: string): Boolean;

var

 vTreeNode: TTreeNode;

 vFieldValues: Variant;

 vFieldNames: string;

begin

 Result := False;

 if not Assigned(mDataSet) then Exit;

 if not Assigned(mTreeView) then Exit;

 if not mDataSet.Active then Exit;

 vFieldNames := Format('%s;%s;%s',

   [mFieldNameParent, mFieldNameTreeText, mFieldNameTreeId]);

 mDataSet.Filtered := False;

 mDataSet.Filter := Format('%s=%s', [mFieldNameParent, QuotedStr(mParentText)]);

 mDataSet.Filtered := True;

 if mDataSet.RecordCount = 0 then Exit;

 mDataSet.First;

 while not mDataSet.Eof do begin

   vTreeNode := mTreeView.Items.AddChild(mTreeNode,

     mDataSet.FieldByName(mFieldNameTreeText).AsString);

   vFieldValues := mDataSet[vFieldNames];

   DataSetToTreeNode(mDataSet, mFieldNameParent, mFieldNameTreeText,

     mFieldNameTreeId, mTreeView, vTreeNode,

     mDataSet.FieldByName(mFieldNameTreeId).AsString);

   ///////Begin 恢复位置

   mDataSet.Filtered := False;

   mDataSet.Filter := Format('%s=%s', [mFieldNameParent, QuotedStr(mParentText)]);

   mDataSet.Filtered := True;

   mDataSet.Locate(vFieldNames, vFieldValues, []);

   ///////End 恢复位置

   mDataSet.Next;

 end;

 Result := True;

end;

///////End Source

///////Begin Demo

procedure TForm1.Button1Click(Sender: TObject);

begin

 TreeView1.Items.Clear;

 DataSetToTreeNode(Table1, 'ParentTreeId', 'TreeText', 'TreeId',

   TreeView1, nil, 'NULL');

end;

///////End Source

//---------------------------------------------------------------------------

//Borland C++ Builder

//---------------------------------------------------------------------------

///////Begin Source

bool DataSetToTreeNode(TDataSet* mDataSet,

 AnsiString mFieldNameParent, //父节点标识字段名

 AnsiString mFieldNameTreeText, //节点文本字段名

 AnsiString mFieldNameTreeId, //节点标识字段名

 TTreeView* mTreeView, TTreeNode* mTreeNode,

 AnsiString mParentText)

{

 TTreeNode* vTreeNode;

 Variant vFieldValues;

 AnsiString vFieldNames;

 if (mDataSet == NULL) return false;

 if (mTreeView == NULL) return false;

 if (!mDataSet->Active) return false;

 vFieldNames = Format("%s;%s;%s",

   ARRAYOFCONST((mFieldNameParent, mFieldNameTreeText, mFieldNameTreeId)));

 mDataSet->Filtered = false;

 mDataSet->Filter = Format("%s=%s", ARRAYOFCONST((mFieldNameParent, QuotedStr(mParentText))));

 mDataSet->Filtered = true;

 if (mDataSet->IsEmpty()) return false;

 mDataSet->First();

 while (!mDataSet->Eof) {

   vTreeNode = mTreeView->Items->AddChild(mTreeNode,

     mDataSet->FieldByName(mFieldNameTreeText)->AsString);

   vFieldValues = mDataSet->FieldValues[vFieldNames];

   DataSetToTreeNode(mDataSet, mFieldNameParent, mFieldNameTreeText,

     mFieldNameTreeId, mTreeView, vTreeNode,

     mDataSet->FieldByName(mFieldNameTreeId)->AsString);

   ///////Begin 恢复位置

   mDataSet->Filtered = false;

   mDataSet->Filter = Format("%s=%s", ARRAYOFCONST((mFieldNameParent, QuotedStr(mParentText))));

   mDataSet->Filtered = true;

   mDataSet->Locate(vFieldNames, vFieldValues, TLocateOptions());

   ///////End 恢复位置

   mDataSet->Next();

 };

 return true;

} /* DataSetToTreeNode */

///////End Source

///////Begin Demo

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 TreeView1->Items->Clear();

 DataSetToTreeNode(Table1, "UpperTreeId", "TreeName", "TreeId",

   TreeView1, NULL, "NULL");

}

///////End Demo