asp教程.net c# 删除节点与节点重命名代码
private void menuitemdelete_click(object sender, system.eventargs e)
{
try
{
if(messagebox.show(this,"删除节点<"+treeview.selectednode.text.trim()+">,与其对应的信息也将被删除,你确定要删除吗?","information",messageboxbuttons.okcancel,messageboxicon.question)==dialogresult.ok)
{
//判断选定的节点是否存在下一级节点
if ( treeview.selectednode.nodes.count == 0 )
{
//删除节点
treenode delenode=treeview.selectednode;
if (file.exists(this.richtextboxpicturename)) file.delete(this.richtextboxpicturename);
int rows=mybindtree.position;
treeview.nodes.remove(delenode);
dataset.tables["tree"].rows[rows].delete();
}
else
messagebox.show ( "请先删除此节点中的子节点!" , "information" , messageboxbuttons.ok , messageboxicon.information ) ;
}
}
catch(exception te)
{
messagebox.show(this,te.message,"errordelete",messageboxbuttons.ok,messageboxicon.warning);
}
}
6、节点重命名
private void menuitemrename_click(object sender, system.eventargs e)
{
treenode myselectednode=treeview.selectednode;
if (myselectednode!=null)
{
treeview.selectednode=myselectednode;
treeview.labeledit=true;
myselectednode.beginedit();
}
else
messagebox.show("no tree node selected .n","invalid selection");
}
/// <summary>
/// 返回指定表的最大nodeid号码,也就是当前选定节点的tag
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="columnname">数据列</param>
/// <returns>最大nodeid号码</returns>
private int maxrecordnumber(datatable dt,string columnname)
{
int number=0;
dataview dv=dt.defaultview;
dv.sort=columnname+" asc";//一个字符串,它包含列名,后跟"asc"(升序)或"desc"(降序)。在默认情况下,列按升序排列。多个列可用逗号隔开。
foreach(datarow dr in dt.rows)
{
number=convert.toint16(dr[columnname].tostring().trim())>number?convert.toint16(dr[columnname].tostring().trim()):number;
}
return number;
}
/// <summary>
/// 在数据表中增加节点或删除节点
/// </summary>
/// <param name="adddeletenode">要增加或删除的节点</param>
/// <param name="parentnodeid">要增加或删除节点的父节点</param>
/// <param name="adddeletetag">增删节点标志(add为增加节点,delete为删除节点,不区分大小写)</param>
private void adddeletenode(treenode adddeletenode,string parentnodeid,string adddeletetag)
{
try
{
if(adddeletetag.toupper()=="add")
{
object key=parentnodeid;
datarow dr=dataset.tables["tree"].rows.find(key);
datarow drnew=dataset.tables["tree"].newrow();
if(dr!=null)
{
drnew["parentid"]=dr["nodeid"];
}
else
{
drnew["parentid"]=rootnodetag;
}
drnew["nodeid"]=(int)adddeletenode.tag;
drnew["nodename"]=adddeletenode.text.trim();
dataset.tables["tree"].rows.add(drnew);
}
else
{
if(adddeletetag.toupper()=="delete")
{
dataset.tables["tree"].rows[mybindtree.position].delete();
}
}
}
catch(exception te)
{
messagebox.show(this,te.message,"error",messageboxbuttons.ok,messageboxicon.warning);
}
}