首页 > 开发 > 综合 > 正文

动态加载树----treeView

2024-07-21 02:17:13
字体:
来源:转载
供稿:网友
动态加载树

陈根发



命题:从数据库读取字段值作为树的节点



实现:

一.把字段值取出放到dataset 中

二.添加一个根节点

三.添加相应的子节点



代码如下

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;



namespace windowsapplication3

{

/// <summary>

/// form1 的摘要说明。

/// </summary>

public class form1 : system.windows.forms.form

{

private system.windows.forms.treeview treeview1;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private system.componentmodel.container components = null;



public form1()

{

//

// windows 窗体设计器支持所必需的

//

initializecomponent();



//

// todo: 在 initializecomponent 调用后添加任何构造函数代码

//

}



/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}



#region windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void initializecomponent()

{

this.treeview1 = new system.windows.forms.treeview();

this.suspendlayout();

//

// treeview1

//

this.treeview1.dock = system.windows.forms.dockstyle.bottom;

this.treeview1.imageindex = -1;

this.treeview1.location = new system.drawing.point(0, 137);

this.treeview1.name = "treeview1";

this.treeview1.selectedimageindex = -1;

this.treeview1.size = new system.drawing.size(352, 136);

this.treeview1.tabindex = 0;

this.treeview1.afterselect += new system.windows.forms.treevieweventhandler(this.treeview1_afterselect);

//

// form1

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.drawing.size(352, 273);

this.controls.add(this.treeview1);

this.name = "form1";

this.text = "form1";

this.load += new system.eventhandler(this.form1_load);

this.resumelayout(false);



}

#endregion



/// <summary>

/// 应用程序的主入口点。

/// </summary>

[stathread]

static void main()

{

application.run(new form1());

}



private void treeview1_afterselect(object sender, system.windows.forms.treevieweventargs e)

{



}



private void form1_load(object sender, system.eventargs e)

{

showform();

}

private void showform ()

{

treeview1.nodes.clear() ;

system.windows.forms.treenode mnode=new treenode("全部");

//mnode.imageindex =1;

treeview1.nodes.add(mnode) ;

//假设dataset 已经取得

system.windows.forms.treenode nnode=new treenode("在职");

system.windows.forms.treenode pnode=new treenode("离职");

treeview1.topnode.nodes.add(nnode) ;

treeview1.topnode.nodes.add(pnode) ;

treeview1.select ();

if (ds.tables [0].rows.count <=0 )

{

return;

}

else

{

// add add some childtreenodes



foreach(datarow dr in ds.tables [0].rows )

{



system.windows.forms.treenode anode=new treenode(dr["name"].tostring() );

system.windows.forms.treenode bnode=new treenode(dr["name"].tostring());

treeview1.nodes [0].nodes[0].nodes .add (anode);

treeview1.nodes [0].nodes[1].nodes .add (bnode);

}

}

}



}

}

好了,节点的值就是字段值!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表