首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

treeview显示数据库的记录,该如何解决

2012-03-05 
treeview显示数据库的记录小弟刚学bcb,想在程序中实现treeview显示数据库的记录,来作为导航,哪位大哥能不

treeview显示数据库的记录
小弟刚学bcb,想在程序中实现treeview显示数据库的记录,来作为导航,哪位大哥能不能给个实例给我参考一下,十分谢谢!!!

[解决办法]
DataModule1->ADOConnection1->LoginPrompt=false;
DataModule1->ADOConnection1->Open("Admin","");
//建立目录树结构
DataModule1->ADOQuery1->Close();
DataModule1->ADOQuery1->SQL->Clear();
DataModule1->ADOQuery1->SQL->Text="select FLNo,FLName,FLIndex,FLPIndex from 材料分类表";
DataModule1->ADOQuery1->Open();
try
{
ss=new TStringList();
TreeView1->Items->BeginUpdate();//禁止刷新,提高速度
//遍历记录集
for(DataModule1->ADOQuery1->First(); !DataModule1->ADOQuery1->Eof; DataModule1->ADOQuery1->Next())
{
ndata=new NODEDATA();
ndata->id = DataModule1->ADOQuery1->FieldByName("FLIndex")->AsInteger;
ndata->pid = DataModule1->ADOQuery1->FieldByName("FLPIndex")->AsInteger;
ndata->name =DataModule1->ADOQuery1->FieldByName("FLName")->AsString;
ndata->memo = DataModule1->ADOQuery1->FieldByName("FLNo")->AsString;
//生成一个根节点加入TreeView
aNode=TreeView1->Items->AddObject(NULL,ndata->name,ndata);
//记录id-TreeNode对应关系,便于下面查找
ss->AddObject(AnsiString(ndata->id),aNode);
}
DataModule1->ADOQuery1->Close();
ndata=NULL;
//下面是关键!!
//调整树,根据节点的pid,把节点移到相应的TreeNode下
int idx;
for(aNode=TreeView1->Items->GetFirstNode(); aNode!=NULL ;)
{
ndata = (NODEDATA*)(aNode->Data);
if(-1==(idx=ss->IndexOf(AnsiString(ndata->pid))))
{
aNode=aNode->getNextSibling();
continue;
}
else
{
pNode = (TTreeNode*)(ss->Objects[idx]);
bNode=aNode;
aNode=aNode->getNextSibling();
bNode->MoveTo(pNode,naAddChild);
}
}
}
__finally
{
//delete ss; ss=NULL;
//DataModule1->ADOQuery1->Close();
//DataModule1->ADOConnection1->Close();
TreeView1->Items->EndUpdate();
//aNode= (TTreeNode*)(ss->Objects[2]);
TreeView1->Selected=(TTreeNode*)(ss->Objects[2]);
}
//调入材料表
//DataModule1->ADOConnection1->LoginPrompt=false;
//DataModule1->ADOConnection1->Open("Admin","");
//Module1->ADOQuery1->SQL->Clear();
//DataModule1->ADOQuery1->SQL->Text="select FL_No,FL_Name,FL_Index,FL_PIndex from 材料分类表";
//DataModule1->ADOQuery1->Open();
AnsiString s;
//idx=TreeView1->Selected->AbsoluteIndex;
//ndata = (NODEDATA*)(aNode->Data);
//S=ss->IndexOf(AnsiString(ndata->pid)))
ndata = (NODEDATA*)(TreeView1->Selected->Data);
s=ndata->memo;
s="where 材料编号 like '"+s+"%'";
DataModule1->ADOQuery2->Close();
DataModule1->ADOQuery2->SQL->Clear();
DataModule1->ADOQuery2->SQL->Text="select 材料编号,材料名称,规格,单位,单价,类别 ";
DataModule1->ADOQuery2->SQL->Add(" from 材料基价表");
DataModule1->ADOQuery2->SQL->Add(s);
DataModule1->ADOQuery2->Open();

热点排行