C++Builder高手进阶 (三)用BCB设计DBTreeView组件(续一)

王朝c/c++·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

//DBTreeView详细设计.CPP文件

__fastcall TDBTreeView::TDBTreeView(TComponent* Owner)

: TTreeView(Owner)

{

//在构造函数中对相关数据进行初始化

FDataLink = new TFieldDataLink; //建立数据连接类

FParentIDField = ""; //将节点父标识设置为空

FPrimaryIDField = ""; //将节点主标识设置为空

FDisplayField = ""; //将显示字段设置为空

FActive = false; //设置为非活动状态

FAllowModifyDB = false; //设置数据集不允许修改

}

__fastcall TDBTreeView::~TDBTreeView()

{//在析构函数中释放构造函数分配的资源

delete FDataLink; //释放数据连接类

}

void __fastcall TDBTreeView::SetActive(bool Value)

{

//设置状态是否为活动

if (FActive != Value) //如果指定状态与当前状态一样,则不做处理,直接返回

{//否则

if (Value)//如果指定值为true则

{

if (FDataLink->DataSource == NULL)//如果未指定数据源则抛出一个异常

throw Exception("Missing DataSource property.");

if (FDataLink->DataSource->DataSet == NULL)//否则,如果数据源中未指定数据集,也抛出一个异常

throw Exception("Invalid DataSource.");

if (FParentIDField == "")//如果父标识字段为空,抛出一个异常

throw Exception("Missing ParentIDField property.");

if (FPrimaryIDField == "")//如果主标识字段为空,抛出一个异常

throw Exception("Missing PrimaryIDField property.");

if (FDisplayField == "")//如果显示字段为空抛出一个异常

throw Exception("Missing DisplayField property.");

//补充说明:从以上几个异常您应该不难看出我们这个组件需要的环境:

//必须指定数据源,数据集,节点父标识字段,主标识字段,显示字段

ClearAllNodes();//删除原来所有节点

FillTreeNodes(0, NULL);//填充所有数据到各节点

}

else

{//如果指定值为false则

ClearAllNodes(); //删除所有节点

}

FActive = Value; //将指定值赋给FActive

}

}

void __fastcall TDBTreeView::SetDataSource(Db::TDataSource* Value)

{//设置数据源

if (Value != FDataLink->DataSource)//如果指定值与当前值不一样,则

{

FDataLink->DataSource = Value; //将数据源当前值设置为指定值

}

}

TDataSource* __fastcall TDBTreeView::GetDataSource()

{//获取数据源

return FDataLink->DataSource; //返回与数据源

}

void __fastcall TDBTreeView::ClearAllNodes()

{//删除所有节点,调用TreeView的相关方法进行遍历式节点删除

for (int i=0; i<this->Items->Count; i++)

delete (TDBTreeNodeData*)(Items->Item[i]->Data);

this->Items->BeginUpdate();

this->Items->Clear();

this->Items->EndUpdate();

}

void __fastcall TDBTreeView::FillTreeNodes(int ParentID, TTreeNode* Node)

{//填充数据到节点

TQuery* AQuery = new TQuery(this);//创建一个数据集控件

AnsiString strSql, strText;

int iID, iParentID;

int iImageIndex, iSelectedIndex;

TTreeNode* TreeNode;

TDBTreeNodeData NodeData;

//用指定条件打开相关数据表

AQuery->DatabaseName = ((TTable*)(FDataLink->DataSource->DataSet))->DatabaseName;

AQuery->Close();//关闭数据集

AQuery->SQL->Clear();//清空原查询条件

strSql = "SELECT * FROM " + ((TTable*)(FDataLink->DataSource->DataSet))->TableName + " WHERE ";

strSql += FParentIDField + "=:PID";

AQuery->SQL->Add(strSql);//指定新的查询条件

AQuery->ParamByName("PID")->AsInteger = ParentID;//取得父标识字段内容

AQuery->Open();//打开数据集

this->Items->BeginUpdate();//准备更新DBTreeView显示

while (!AQuery->Eof)//数据没有到数据集尾,则

{

strText = AQuery->FieldByName(FDisplayField)->AsString;//取得显示字段内容

iID = AQuery->FieldByName(FPrimaryIDField)->AsInteger;//取得主标识字段内容

iParentID = AQuery->FieldByName(FParentIDField)->AsInteger;//取得父标识字段内容

TreeNode = this->Items->AddChild(Node, strText);//添加此子节点

iImageIndex = iSelectedIndex = -1;//将节点位图与选中时位图设置为空(-1)

if (FOnSetImageIndex) FOnSetImageIndex(this, iID, iParentID, TreeNode->Level, iImageIndex, iSelectedIndex);//如果位图设置发生变化,则触发相关事件

TreeNode->ImageIndex = iImageIndex;//设置节点位图及选中时的位图

TreeNode->SelectedIndex = iSelectedIndex;

NodeData.ID = iID;//设置节点标识及父标识

NodeData.ParentID = iParentID;

AddDataToNode(TreeNode, NodeData);//追加数据到指定节点

FillChildTreeNodes(iID, TreeNode, false);//填充子节点

AQuery->Next(); //移到下条记录

}

this->Items->EndUpdate(); //更新DBTreeView组件

AQuery->Close(); //关闭数据集

delete AQuery; //删除临时创建的数据集控件

}

《未完待续》

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航