关闭

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

704人阅读 评论(0) 收藏 举报

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();
      
          
        }
             
  
    }
}

posted on 2007-06-27 15:46  
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:392304次
    • 积分:7232
    • 等级:
    • 排名:第3193名
    • 原创:325篇
    • 转载:71篇
    • 译文:1篇
    • 评论:34条
    最新评论