privateDataSet ds;privateSqlDataAdapter sqlDataAdapter1;private intmaxnodeid;private void Form1_Load(objectsender, System.EventArgs e)
{string strconn=ConfigurationSettings.AppSettings["ConnStr"];
sqlConnection1= newSqlConnection(strconn);this.sqlConnection1.Open();//填充DataSet
this.CreateDataSet();//从数据库中读取数据,通过递归生成树。
InitTree(this.treeView1.Nodes,"0");
}private voidCreateDataSet()
{this.sqlDataAdapter1=new SqlDataAdapter("select * from s_menu",this.sqlConnection1);this.ds=newDataSet();this.sqlDataAdapter1.Fill(ds,"tree");
}private void InitTree(TreeNodeCollection Nds,stringparentId)
{
DataView dv=newDataView();
TreeNode tmpNd;stringintId;
dv.Table=ds.Tables["tree"];
dv.RowFilter="ParentId='" + parentId + "'";foreach(DataRowView drv indv)
{
tmpNd=newTreeNode();
tmpNd.Tag=drv["NodeId"].ToString();
tmpNd.Text=drv["NodeName"].ToString();
Nds.Add(tmpNd);
intId=drv["ParentId"].ToString();
InitTree(tmpNd.Nodes,tmpNd.Tag.ToString());
}
}//新增节点操作
private void insert(stringtype)
{//判断是新增树节点,还是子节点.
string strinsert="insert into s_menu values('{0}','{1}','{2}')";string strformat="";if(type=="sub")