1、加载树节点的方法(无级限)
/// <summary>
/// 加载树节点。建树的基本思路是:从根节点开始递归调用显示子树。
/// </summary>
/// <param name="dt">是DataTable类型的保存树节点的数据表</param>
/// <param name="nodeID">数据表中保存节点的列名称</param>
/// <param name="parentID">数据表中保存节点父节点的列名称</param>
/// <param name="nodeName">数据表中保存节点名称的列名称</param>
/// <param name="treeNodeCollection">表示TreeView.Nodes对象的集合</param>
/// <param name="rootNodeTag">定义根节点的父节点的标记</param>
//法一:用DataView
public static void InitTree(DataTable dt,string nodeID,string parentID,string nodeName,TreeNodeCollection treeNodeCollection,string rootNodeTag)
{
try
{
TreeNode tmpNode;
DataView dv=new DataView();
dv.Table=dt;
if (rootNodeTag!=""||rootNodeTag!=null)
dv.RowFilter=parentID+"='"+rootNodeTag+"'";
foreach(DataRowView drv in dv)
{
tmpNode=new TreeNode();
tmpNode.Text=drv[nodeName].ToString();
tmpNode.Tag=drv[nodeID];
string father=drv[nodeID].ToString();
treeNodeCollection.Add(tmpNode);
InitTree(dt,nodeID,parentID,nodeName,tmpNode.Nodes,father);
}
}
catch(Exception te)
{
MessageBox.Show(te.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
注意:以下为弹出菜单项代码及相关的函数和方法
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);
}
}
5、刪除节点
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);
}
}