C 之Winform中treeview控件绑定数据库

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
private DataSet ds;private SqlDataAdapter sqlDataAdapter1;private int maxnodeid;private void Form1_Load(object sender, System.EventArgs e){string strconn=ConfigurationSettings.AppSettings["ConnStr"];sqlConnection1 = new SqlConnection(strconn);this.sqlConnection1.Open();//填充DataSetthis.CreateDataSet();//从数据库中读取数据,通过递归生成树。InitTree(this.treeView1.Nodes,"0");}private void CreateDataSet(){this.sqlDataAdapter1=new SqlDataAdapter("select * from s_menu ",this.sqlConnection1);this.ds=new DataSet();this.sqlDataAdapter1.Fill(ds,"tree");}private void InitTree(TreeNodeCollection Nds,string parentId){DataView dv=new DataView();TreeNode tmpNd;string intId;dv.Table=ds.Tables["tree"];dv.RowFilter="ParentId='" + parentId + "'" ;foreach(DataRowView drv in dv){tmpNd=new TreeNode();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(string type){//判断是新增树节点,还是子节点.string strinsert="insert into s_menu values('{0}','{1}','{2}')";string strformat="";if(type=="sub")strformat=string.Format(strinsert,maxnodeid.ToString(),this.selectnode.Tag.ToString(),this.strcomm);elsestrformat=string.Format(strinsert,maxnodeid.ToString(),"0",this.strcomm);SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);cmd.ExecuteNonQuery();}//为新增节点算出最大的节点值,并以此值作为新增的节点ID值private int GetMaxNodeid(){int pre=0,last=0;DataSet maxds=new DataSet();this.sqlDataAdapter1=new SqlDataAdapter("select nodeid from s_menu order by nodeid",this.sqlConnection1);this.sqlDataAdapter1.Fill(maxds);for(int i=0;i{if(i+1{pre=int.Parse(maxds.Tables[0].Rows[i][0].ToString());last=int.Parse(maxds.Tables[0].Rows[i+1][0].ToString());if(last-pre!=1)return pre+1;}}return last+1;}private void getallnode(TreeNode tn){foreach(TreeNode node in tn.Nodes){list.Add(node.Tag.ToString());if(node.Nodes.Count>0){getallnode(node);}}}private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){//判断是否点击了某个节点this.selectnode= this.treeView1.GetNodeAt (e.X ,e.Y );if(selectnode==null)this.isselected=false;elsethis.isselected=true;}private void menuAdd_Click(object sender, System.EventArgs e){//判断是否点击了某个节点,若没有点击了,则是新增一个树节点if(isselected==false){//算出新增树节点的ID值maxnodeid=GetMaxNodeid();TreeNode tmpNd=new TreeNode();//赋值tmpNd.Tag=this.maxnodeid.ToString();FormCommon frmCommon=new FormCommon();DialogResult result= frmCommon.ShowDialog();if(result==DialogResult.OK){//取到新增树节点的文本值tmpNd.Text=frmCommon.strcomm;this.strcomm=frmCommon.strcomm;//新增树节点this.treeView1.Nodes.Add(tmpNd);//插入数据库(说明插入的是树节点)this.insert("root");//展开this.selectnode.Expand();}}else{//判断是否点击了某个节点,若点击了,则是新增一个子节点this.contextAddSub();}}private void contextAddSub(){//得到新增子节点的ID值maxnodeid=GetMaxNodeid();TreeNode tmpNd=new TreeNode();//赋值tmpNd.Tag=this.maxnodeid.ToString();FormCommon frmCommon=new FormCommon();DialogResult result= frmCommon.ShowDialog();if(result==DialogResult.OK){//取到新增树节点的文本值tmpNd.Text=frmCommon.strcomm;this.strcomm=frmCommon.strcomm;//新增子节点this.selectnode.Nodes.Add(tmpNd);//插入数据库(说明插入的是子节点)this.insert("sub");//展开this.treeView1.SelectedNode.Expand();}}//删除节点操作private void menuDel_Click(object sender, System.EventArgs e){//新建一个ArrayList,用于保存要删除的节点下边的所有子节点list=new ArrayList();if(this.isselected==true){//得到删除的节点下边的所有子节点getallnode(this.selectnode);//把要删除的节点也加进去list.Add(this.selectnode.Tag.ToString());//循环从数据库中删除for(int i=0;i{string strdel="delete s_menu where nodeid='{0}'";string strformat="";strformat=string.Format(strdel,list[i]);SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);cmd.ExecuteNonQuery();}//从树中删除this.selectnode.Remove();}}//修改节点的值private void menuEdit_Click(object sender, System.EventArgs e){if(this.isselected==true){FormCommon frmCommon=new FormCommon();DialogResult result= frmCommon.ShowDialog();if(result==DialogResult.OK){string strdel="update s_menu set nodename= '{1}' where nodeid='{0}'";string strformat="";strformat=string.Format(strdel,this.selectnode.Tag.ToString(),frmCommon.strcomm);SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);cmd.ExecuteNonQuery();this.selectnode.Text=frmCommon.strcomm;}}}//遍历所有节点.查找值private void getvaluenode(TreeNodeCollection tn,string value){foreach(TreeNode node in tn){if(node.Nodes.Count>0){getvaluenode(node.Nodes,value);}if(node.Text==value)listnode.Add(node);}}private void menuSearch_Click(object sender, System.EventArgs e){int j,k;this.listnode=new ArrayList();FormCommon frmCommon=new FormCommon();DialogResult result= frmCommon.ShowDialog();if(result==DialogResult.OK){TreeNode n =new TreeNode();TreeNode temp=new TreeNode();//下面的函数是填充listnode;getvaluenode(this.treeView1.Nodes,frmCommon.strcomm);for(int i=0;i{j=0;k=0;n=(TreeNode)listnode[i];if (n != null){temp=n;//得到上面结点的数量,并将数量保存到变量j;for(;n.Parent!=null;){n=n.Parent;j++;}//恢复原值n=temp;//新建一个树结点数组做保存得到查询到的所有节点.TreeNode[] m=new TreeNode[j];for(;n.Parent!=null;){n=n.Parent;m[k]=n;k++;}for(int p=0;pm[p].Expand();n=temp;n.ForeColor=Color.Red;}}}}private void treeView1_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e){if(this.treeView1.SelectedNode.Text!=null){string strdel="update s_menu set nodename= '{1}' where nodeid='{0}'";string strformat="";strformat=string.Format(strdel,this.treeView1.SelectedNode.Tag.ToString(),e.Label.ToString());SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);cmd.ExecuteNonQuery();}}private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e){this.listBox1.Items.Clear();this.listBox1.Items.Add(this.treeView1.SelectedNode.FullPath.ToString());}}} 


转自Internet,原作者已佚

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值