asp教程.net c#treeview 增加节点
void creat_node(string
name,microsoft.web.ui.webcontrols.treenode t1,string number)
{
//定义一个新的结点
microsoft.web.ui.webcontrols.treenode t2=new
microsoft.web.ui.webcontrols.treenode();
t2.id=number;
t2.text=name;
//判断是否为根结点
treeview1.expandedimageurl="images/depart03.bmp";
if(t1.id.trim()=="###")
{
treeview1.nodes.add(t2);
t2.imageurl="images/depart01.bmp";
}
else
{
t1.nodes.add(t2);
t2.imageurl="images/depart01.bmp";
t1.imageurl="images/depart02.bmp";
}
//取得这个结点的所有子结点,利用递归自动生成
string myselectquery = "select parentid, id,name from table1
where parentid='" + number +"'";
string myconnstring="provider=microsoft.jet.oledb.4.0;data
source="+server.mappath(".")+"/dt/mydata.mdb";
oledbconnection myconnection = new
oledbconnection(myconnstring);
oledbcommand mycommand = new
oledbcommand(myselectquery,myconnection);
myconnection.open();
oledbdatareader myreader;
myreader = mycommand.executereader();
while (myreader.read())
{
creat_node(myreader.getstring(2),t2,myreader.getstring(1));
}
myreader.close();
myconnection.close();
}
private void createprovidertreebtn_click(object sender,
system.eventargs e)
{
microsoft.web.ui.webcontrols.treenode t1=new
microsoft.web.ui.webcontrols.treenode();
creat_node("",t1,"###");
2、增加根节点
private void menuitemaddroot_click(object sender, system.eventargs e)
{
try
{
treenode treenode=new treenode("新节点");
treenode.tag=maxrecordnumber(dataset.tables["tree"],"nodeid")+1;
this.treeview.nodes.add(treenode);
adddeletenode(treenode,rootnodetag,"add");
treeview.selectednode = treenode ;
loadtreeview.loadtreenodeimage(treeview.selectednode,this.imagelist1,nodeimagemode);
//执行指定节点的选定操作
this.treeviewafterselect(treeview.selectednode );
menuitemrename_click(new object(),new system.eventargs());
}
catch(exception te)
{
messagebox.show(this,te.message,"errorroot",messageboxbuttons.ok,messageboxicon.warning);
}
}
3、增加子节点
private void menuitemaddchild_click(object sender, system.eventargs e)
{
try
{
//1.当一个节点有对应链接资料时,不可以增加子节点
//2.当一个节点有对应链接资料时,可以增加图片
if (this.ishavelinedata(dataset.tables["data"],this.richtextboxid))
{
messagebox.show(this,"要增加子节点,必须在这个节点没有可以链接的资料后才可以增加!","操作错误",messageboxbuttons.ok,messageboxicon.information );
return;
}
treenode treenode=new treenode("新节点");
treenode.tag=maxrecordnumber(dataset.tables["tree"],"nodeid")+1;
treeview.selectednode.nodes.add(treenode);
adddeletenode(treenode,treeview.selectednode.tag.tostring(),"add");
treeview.selectednode = treenode ;
loadtreeview.loadtreenodeimage(treeview.selectednode,this.imagelist1,nodeimagemode);
//执行指定节点的选定操作
this.treeviewafterselect(treeview.selectednode );
menuitemrename_click(new object(),new system.eventargs());
}
catch(exception te)
{
messagebox.show(this,te.message,"errorchild",messageboxbuttons.ok,messageboxicon.warning);
}
}
4、增加兄弟节点
private void menuitemaddborther_click(object sender, system.eventargs e)
{
try
{
//判断是否选定组件中的根节点
if (treeview.selectednode.parent==null)
{
messagebox.show ("节点<"+treeview.selectednode.text.trim()+ ">为根节点,要加入兄弟节点,请使用"加入根节点"选项!" , "information" , messageboxbuttons.ok , messageboxicon.information ) ;
return;
}
treenode treenode=new treenode("新节点");
treenode.tag=maxrecordnumber(dataset.tables["tree"],"nodeid")+1;
treeview.selectednode.parent.nodes.add(treenode);
adddeletenode(treenode,treeview.selectednode.parent.tag.tostring().trim(),"add");
treeview.selectednode = treenode ;
loadtreeview.loadtreenodeimage(treeview.selectednode,this.imagelist1,nodeimagemode);
//执行指定节点的选定操作
this.treeviewafterselect(treeview.selectednode );
menuitemrename_click(new object(),new system.eventargs());
}
catch(exception te)
{
messagebox.show(this,te.message,"errorparent",messageboxbuttons.ok,messageboxicon.warning);
}
}
contextmenustrip1与treeview1关联,在节点上单击右键就给该节点添加子节点,在空白地方单击右键,就添加根节点
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
namespace windowsapplication8
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
this.treeview1.labeledit = true;
}
private void treeview1_nodemouseclick(object sender, treenodemouseclickeventargs e)
{
if (e.button == mousebuttons.right)
{
this.treeview1.selectednode = e.node;
flag = true;
contextmenustrip1.show(mouseposition);
}
}
bool flag = false;
private void 添加节点toolstripmenuitem_click(object sender, eventargs e)
{
treenode tn = new treenode();
if (flag)
{
this.treeview1.selectednode.nodes.add(tn);
this.treeview1.selectednode = tn;
tn.beginedit();
flag = false;
}
else
{
this.treeview1.nodes.add(tn);
this.treeview1.selectednode = tn;
tn.beginedit();
flag = false;
}
}
private void treeview1_afterlabeledit(object sender, nodelabelediteventargs e)
{
e.node.text = e.label;
}
private void contextmenustrip1_opening(object sender, canceleventargs e)
{
if (!flag)
{
this.treeview1.selectednode = null;
}
}
bool flag1 = false;
private void contextmenustrip1_itemclicked(object sender, toolstripitemclickedeventargs e)
{
flag1 = true;
}
private void contextmenustrip1_closed(object sender, toolstripdropdownclosedeventargs e)
{
if (!flag1)
{
flag = false;
}
else
{
flag1 = false;
}
}
}
}