不用第归算法快速显示树,对于Oracle数据库(2)
2024-07-21 02:23:50
供稿:网友
namespace bq_treeview
{/// <summary>
/// bq_treeview 的摘要说明
/// </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>
///定义数据源
/// </summary>
public datatable treedatasource
{
set
{
m_tb=value;
}
}
/// <summary>
///清空所有树控件中的信息
/// </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>
/// 构造树控件
/// </summary>
public void maketree()
{
clear();//清除树节点中的资源
cursor.current =cursors.waitcursor ;
// 首先要给根节点付值,因为取出来的结构中没有根节点的信息
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>
/// 构造一个动态数组,模拟树控件一个分叉的线性结构,每一次都是最新的线性结构,这整个控件关键地方
/// </summary>
/// <param name="strid">层次</param>
/// <param name="matid">物料编码</param>
/// <param name="nodef">父节点</param>
/// <returns>构造的节点</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;
}
}
}
}
经过编译就可以把控件bq_treeview添加进来,如下图(图2)
在应用程序中只要
编写
bq_treeview1.rootmatid=strrootid;
bq_treeview1.treedatasource=tbproinfo;
bq_treeview1.maketree();
就可以显示整个bom结构,tbproinfo为上面的sql语句取出的表结构