VS2005[C#] 轻轻松松 掌握 TreeView [ 回车展开节点, 鼠标右键,禁止闪屏....]

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.Data.SqlClient;
namespace  ksClient
{
    
public partial class DGM : Form
    
{
        
//获取当前选定的节点值
        public int CurrentVal = 0;

        
//获取控件树的深度
        public int pubDeep = 0;

        
// 获取子树节点的个数
        public int pubChildNum = 0;
               
        
//获取当前节点对象
        public TreeNode CurrentNode = null;

        
//设置 对话框
        public DialogResult dr;

        
public DataSet ds1 = null;
        
public DataSet ds2 = null;
        
public DataSet ds3 = null;
        
public DataSet ds4 = null;
      
        
public DGM()
        
{
            InitializeComponent();
        }


        
private DataSet  Bind(string sql)
        
{
            SqlConnection conn 
= new SqlConnection("Server=.;database=model;Integrated Security=SSPI");
            SqlDataAdapter da 
= new SqlDataAdapter(sql,conn);
            DataSet ds 
= new DataSet();
            da.Fill(ds);
            
return ds;
            
        }

        
private void KK(string sql)
        
{
            SqlConnection conn 
= new SqlConnection("Server=.;database=model;Integrated Security=SSPI");
            SqlCommand cmd 
= new SqlCommand(sql, conn);
            
try
            
{
                conn.Open();
                cmd.ExecuteNonQuery();
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }

        
protected  void inittree()
        
{
            tvShow.Nodes.Clear();


            DataView dv 
= Bind("select *from series").Tables[0].DefaultView;
            
            ds1 
= Bind("select * from groups");
            ds2 
= Bind("select *from subject");
            ds3 
= Bind("select * from chapter");
            ds4 
= ds3.Copy();
          
//  ds4 = Bind("select * from chapter");

            
if (dv.Table.Rows.Count > 0)
            
{
                
foreach (DataRowView dr in dv)
                
{
                   

                    TreeNode tn 
= new TreeNode();
                    tn.Text 
= dr[2].ToString();
                    tn.Tag 
= dr[0].ToString();
                    
                  
                    tvShow.Nodes.Add(tn);
                    childnode_1(tn);
                   
                }

            }

   

        }


        
protected void childnode_1(TreeNode tn)
        
{
            
int second =Int32.Parse( tn.Tag.ToString());
            DataView dv 
= ds1.Tables[0].DefaultView;
            
if (dv.Table.Rows.Count > 0)
            
{
                dv.RowFilter 
= "SID=" + second;
                
if (dv.Count > 0)
                
{
                    
foreach (DataRowView dr in dv)
                    
{
                        TreeNode tn_2 
= new TreeNode();
                        tn_2.Text 
= dr[2].ToString();
                        tn_2.Tag 
= dr[0].ToString();
                      
                        tn.Nodes.Add(tn_2);
                        childnode_2(tn_2);
                    }

                }

            }

        }

        
protected void childnode_2(TreeNode tn)
        
{
            
int three = Int32.Parse(tn.Tag.ToString());
            DataView dv 
= ds2.Tables[0].DefaultView;
            
if (dv.Table.Rows.Count > 0)
            
{
                dv.RowFilter 
= "GID=" + three;
                
if (dv.Count > 0)
                
{
                    
foreach (DataRowView dr in dv)
                    
{
                        TreeNode tn_3 
= new TreeNode();
                        tn_3.Text 
= dr[2].ToString();
                        tn_3.Tag 
= dr[0].ToString();
                      
                        tn.Nodes.Add(tn_3);
                        
//childnode_3(tn_3);
                       DiGui(ds3.Tables[0],tn_3);
                    }

                }

            }

        }

        
private void DiGui(DataTable dt,TreeNode tn)
        
{
            DataView dv 
= new DataView(dt);

            dv.RowFilter 
= "CInterID=" + Int32.Parse(tn.Tag.ToString());
            
foreach (DataRowView drv in dv)
            
{
                 TreeNode tn_n
=new TreeNode();
                 tn_n.Text 
= drv[2].ToString();
                 tn_n.Tag 
= drv[0].ToString();
                 tn.Nodes.Add(tn_n);
                 DiGui(dt, tn_n);
            }

        }
 
        
protected void childnode_3(TreeNode tn)
        
{
        
            
int four = Int32.Parse(tn.Tag.ToString());
            DataView dv 
= ds3.Tables[0].DefaultView;
            
if (dv.Table.Rows.Count > 0)
            
{
                dv.RowFilter 
= "SID=" + four;

                
if (dv.Count > 0)
                
{
                    
foreach (DataRowView dr in dv)
                    
{
                        TreeNode tn_4 
= new TreeNode();
                        tn_4.Text 
= dr[2].ToString();
                        tn_4.Tag 
= dr[0].ToString();
                      
                        tn.Nodes.Add(tn_4);
                     
                        childnode_4(tn_4);
                    }

                }

            }

        }

        
protected void childnode_4(TreeNode tn)
        
{
            
int five = Int32.Parse(tn.Tag.ToString());
            DataView dv 
= ds4.Tables[0].DefaultView;
            dv.RowFilter 
= "CInterID=" + five;
            
if (dv.Count > 0)
            
{
                
foreach (DataRowView dr in dv)
                
{
                    TreeNode tn_5 
= new TreeNode();
                    tn_5.Text 
= dr[2].ToString();
                    tn_5.Tag 
= dr[0].ToString();
                  
                    tn.Nodes.Add(tn_5);
                }

            }

        }


        
private void DGM_Load(object sender, EventArgs e)
        
{
            inittree();
        }

        
// 选择节点
        private void tvShow_AfterSelect(object sender, TreeViewEventArgs e)
        
{
            CurrentVal 
= Int32.Parse(e.Node.Tag.ToString());
            CurrentNode 
= e.Node;
            pubChildNum 
= e.Node.GetNodeCount(true);
        }


        
private void tvShow_AfterCollapse(object sender, TreeViewEventArgs e)
        
{
            e.Node.ImageIndex 
= 0;
        }

         
        
private void tvShow_AfterExpand(object sender, TreeViewEventArgs e)
        
{
            e.Node.ImageIndex 
= 1;
        }

        
// 鼠标 右键
        private void tvShow_MouseDown(object sender, MouseEventArgs e)
        
{
            
if (e.Button == MouseButtons.Right)
            
{
                TreeNode tn 
= tvShow.GetNodeAt(e.X, e.Y);
                
if (tn != null)
                
{
                    tvShow.SelectedNode 
= tn;
                }

            }

        }

        
// 回 车键 展开节点
        private void tvShow_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
if (e.KeyChar == 13)
            
{
                tvShow.SelectedNode.Expand();
                
            }

          
        }


        
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            
if (pubChildNum > 0)
            
{
                dr 
= MessageBox.Show("节点包含子节点,仍然要删除吗?""确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                
if (dr == DialogResult.Yes)
                
{
                    tvShow.Nodes.Remove(CurrentNode);
                    DeleteNode();
                }

            }

            
else
            
{
                tvShow.Nodes.Remove(CurrentNode);
                DeleteNode();
            }

        }

        
private void DeleteNode()
        
{
            tvShow.BeginUpdate();
//  禁止绘制 树图,目的不让他刷新
            string sqlStr="";
            
if (pubDeep == 0)
            
{
                sqlStr 
= "delete from series where SID=" + CurrentVal;
                KK(sqlStr);
            }

            
else if (pubDeep == 1)
            
{
                sqlStr 
= "delete from groups where GID="+CurrentVal;
                KK(sqlStr);
            }

            
else if (pubDeep == 2)
            
{
                sqlStr 
= "delete from subject where SID=" + CurrentVal;
                KK(sqlStr);
            }

            
else if (pubDeep == 3)
            
{
                sqlStr 
= "delete from chapter where GID=" + CurrentVal;
                KK(sqlStr);
                sqlStr 
= "delete from chapter where CInterID=" + CurrentVal;
                KK(sqlStr);
            }

            
else if (pubDeep == 4)
            
{
                sqlStr 
= "delete from chapter where GID=" + CurrentVal;
                KK(sqlStr);
            }

            tvShow.EndUpdate();
        }


        
private void 添加_Click(object sender, EventArgs e)
        
{
            
            tvShow.BeginUpdate();
//  禁止绘制 树图,目的不让他刷新
            string sqlStr = "";
            
if (pubDeep == 0)
            
{
                sqlStr 
= "insert into groups(GNumber,GName,SID)values('1001-1001','ASPNET'," + CurrentVal + ")";
            }

           
            KK(sqlStr);
            inittree();
            tvShow.EndUpdate();
       
           
        }

              
   
    }

}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值