不用第歸算法快速顯示樹(shù),對(duì)于Oracle數(shù)據(jù)庫(kù)(2)
2024-07-21 02:23:50
供稿:網(wǎng)友
namespace bq_treeview
{/// <summary>
/// bq_treeview 的摘要說(shuō)明
/// </summary>
public class bq_treeview : system.windows.forms.treeview
{
private system.componentmodel.container components = null;
private datatable m_tb=null;
private string m_rootmatid;
bq_treenode rootnode=null;
arraylist alnode=new arraylist ();
public bq_treeview()
{
initializecomponent();
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region
private void initializecomponent()
{
components = new system.componentmodel.container();
}
#endregion
/// <summary>
///定義數(shù)據(jù)源
/// </summary>
public datatable treedatasource
{
set
{
m_tb=value;
}
}
/// <summary>
///清空所有樹(shù)控件中的信息
/// </summary>
private void clear()
{
if(rootnode!=null)
{
rootnode.nodes .clear ();
if(rootnode!=null)
{
try
{
rootnode.remove ();
}
catch
{}
}
}
if(alnode!=null)
if(alnode.count >0)
alnode.clear ();
}
/// <summary>
/// 構(gòu)造樹(shù)控件
/// </summary>
public void maketree()
{
clear();//清除樹(shù)節(jié)點(diǎn)中的資源
cursor.current =cursors.waitcursor ;
// 首先要給根節(jié)點(diǎn)付值,因?yàn)槿〕鰜?lái)的結(jié)構(gòu)中沒(méi)有根節(jié)點(diǎn)的信息
rootnode=new bq_treenode(m_rootmatid);
rootnode.matid =m_rootmatid;
alnode.add(rootnode);
this.nodes .add (rootnode);
foreach(datarow row in m_tb.rows )
{
bq_treenode fnode=null;
bq_treenode cnode=null;
cnode=makearray(row["id"].tostring (),row["物料編碼"].tostring (),out fnode);
fnode.nodes.add (cnode);
}
rootnode.expand ();
cursor.current =cursors.default ;
}
// <summary>
/// 構(gòu)造一個(gè)動(dòng)態(tài)數(shù)組,模擬樹(shù)控件一個(gè)分叉的線(xiàn)性結(jié)構(gòu),每一次都是最新的線(xiàn)性結(jié)構(gòu),這整個(gè)控件關(guān)鍵地方
/// </summary>
/// <param name="strid">層次</param>
/// <param name="matid">物料編碼</param>
/// <param name="nodef">父節(jié)點(diǎn)</param>
/// <returns>構(gòu)造的節(jié)點(diǎn)</returns>。
private bq_treenode makearray(string strid,string matid,out bq_treenode nodef) {
try
{
bq_treenode node=new bq_treenode (matid);
node.matid =matid;
bq_treenode noderet=null;
int ngrade=int.parse (strid);
if(ngrade==0)
{
}
else
{
if(alnode.count>=ngrade+1)
{
alnode[ngrade]=node;
noderet=(bq_treenode)alnode[ngrade-1];
}
else
{
alnode.add (node);
noderet=(bq_treenode)alnode[ngrade-1];
}
}
nodef=noderet;
return node;
}
catch(exception e)
{
throw new exception("",e);
}
}
public string rootmatid
{
get
{
return m_rootmatid;
}
set
{
m_rootmatid=value;
}
}
}
}
經(jīng)過(guò)編譯就可以把控件bq_treeview添加進(jìn)來(lái),如下圖(圖2)
在應(yīng)用程序中只要
編寫(xiě)
bq_treeview1.rootmatid=strrootid;
bq_treeview1.treedatasource=tbproinfo;
bq_treeview1.maketree();
就可以顯示整個(gè)bom結(jié)構(gòu),tbproinfo為上面的sql語(yǔ)句取出的表結(jié)構(gòu)