基本信息
源码名称:winfrom自定义树形
源码大小:0.32M
文件格式:.rar
开发语言:C#
更新时间:2016-03-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TreeControlLibrary;
using SQLHelper;
using System.Threading;
using BusinessInterface;
using BusinessInstance;
using DataMode;

namespace TreeViews
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        ISimpleInfo Sim = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            //接口
            Sim = LogicInstance.NewInfo();

            this.trees.AllowDrop = true;//是否可以进行拖动
            Databinding();

        }

        #region 动态绑定数据

        private void BindTree(TreeNodeCollection nodes, DataTable dataSource, int tag)
        {

            nodes.Clear();
            string filter;
            filter = string.Format("{0}={1}", "ParentID", tag);
            DataRow[] drarr = dataSource.Select(filter);
            TreeNode node;
            foreach (DataRow dr in drarr)
            {
                node = new TreeNode();
                node.Text = (string)dr["RegionName"];
                node.Tag = (int)dr["RegionID"];
                node.Name = (string)dr["LCode"];
                nodes.Add(node);
                BindTree(node.Nodes, dataSource, (int)node.Tag);
            }

        }

        /// <summary>
        /// 动态绑定数据
        /// </summary>
        public void Databinding()
        {
            string error = string.Empty;
            //Sim.BindTreeInfo(mytable, 0, "RegionID", "ParentID", "RegionName", "LCode", ref error);
            DataTable mytable = new DataTable();
            mytable = Sim.GetTable("B_Region", " and DelFlag=0  order by LCode");
            BindTree(trees.Nodes, mytable, 0);
            if (error != string.Empty)
            {
                MessageBox.Show(error);
            }

            trees.ExpandAll();
        }

        #endregion


        #region 节点操作

        /// <summary>
        /// 节点的添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trees_Add(object sender, TreeClass.ChangeMyArgs e)
        {
            string aaa = e.NameInfo;
            Form f = Application.OpenForms["ModifiyNode"];  //查找是否打开过Form1窗体
            if (f == null)  //没打开过
            {
                ModifiyNode fname = new ModifiyNode();
                fname.TypeInfo = e.TypeInfo;
                fname.LCode = e.NameInfo;
                if (fname.ShowDialog() == DialogResult.OK)
                {
                    Databinding();
                }
            }
            else
            {
                f.Focus();   //打开过就让其获得焦点
            }
        }


        /// <summary>
        /// 节点的删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trees_Del(object sender, TreeClass.ChangeMyArgs e)
        {
            string errorinfo = "";
            if (MessageBox.Show("确定删除?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DataTable myTable = new DataTable();
                myTable.TableName = "B_Region";
                myTable = Sim.GetTable("B_Region", " and DelFlag=0 and LCode='"   e.NameInfo   "' order by LCode");
                bool info = Sim.Del(myTable, ref errorinfo);
                if (info)
                {
                    MessageBox.Show("操作成功");
                }
                else
                {
                    MessageBox.Show("操作失败");
                }
                Databinding();
            }
        }

        /// <summary>
        /// 节点的修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trees_Update(object sender, TreeClass.ChangeMyArgs e)
        {
            string errorinfo = "";
            string NewName = e.RegionName;
            DataTable myTable = new DataTable();

            myTable = Sim.GetTable("B_Region", " and DelFlag=0 and LCode='"   e.NameInfo   "' order by LCode");
            myTable.TableName = "B_Region";
            myTable.Rows[0]["RegionName"] = NewName;
            //string sqldel = "update B_Region set RegionName='"   NewName   "' where LCode='"   e.NameInfo   "'";
            if (!Sim.Update(myTable, ref errorinfo))
            {
                MessageBox.Show("操作失败,请刷新后重试");
            }
            //Databinding();
        }


        /// <summary>
        /// 移动节点的保存
        /// </summary>
        /// <param name="NewParentNode">移动最后的新父节点</param>
        /// <param name="ChildNode">移动节点</param>
        /// <returns>0失败-1成功</returns>
        protected int MoveNodes(string NewParentNode, string ChildNode)
        {
            string errorinfo = "";
            DataTable mytable = new DataTable();

            
            int ResNum = 0;
            string FatherLCode = ""; string HighLCode = ""; string NewLCode = "";//修改后的节点名字:父节点 本级最大节点
            string sql = ""; string FNewCode = "";//保存第一次结束以后的父级节点;
            string NewFaherID;
            DataRow[] drarr;
            if (ChildNode != "")
            {
                DataTable dataSource = new DataTable();
                dataSource = SQLHelper.SQLHelper.GetDataTable("select RegionID,ParentID,RegionCode,RegionName,LCode,DelFlag,"
                             "Remark from B_Region where DelFlag=0 and LCode like '"   ChildNode   "%' order by LCode ");
                drarr = dataSource.Select();

                NewFaherID = SQLHelper.SQLHelper.ExecuteScalar("select RegionID from B_Region where LCode='"   NewParentNode   "'").ToString();//新的父ID
                FatherLCode = NewParentNode;

                foreach (DataRow dr in drarr)
                {
                    DataTable newtable = new DataTable();
                    //新的开头编号
                    if (dr["LCode"].ToString() == ChildNode)
                    {

                        try
                        {
                            //第一个节点,改变成当前父节点的最大子节点
                            HighLCode = SQLHelper.SQLHelper.ExecuteScalar("select SUBSTRING(LCode,LEN(LCode)-2,3) from B_Region where LCode like '"   FatherLCode   "___' and LEN(LCode)>3 and DelFlag=0 order by LCode desc").ToString();
                            if ((int.Parse(HighLCode)   1).ToString().Length == 1)
                            {
                                HighLCode = "00"   (int.Parse(HighLCode)   1).ToString();
                            }
                            else if ((int.Parse(HighLCode)   1).ToString().Length == 2)
                            {
                                HighLCode = "0"   (int.Parse(HighLCode)   1).ToString();
                            }
                        }
                        catch (Exception)
                        {
                            HighLCode = "001";
                        }
                        NewLCode = FNewCode = FatherLCode   HighLCode;

                        if (mytable != null)
                        {
                            newtable = Sim.GetTable("B_Region", " and  LCode='"   dr["LCode"].ToString()   "' order by LCode");
                            newtable.Rows[0]["LCode"] = NewLCode;
                            newtable.Rows[0]["ParentID"] = NewFaherID;
                            mytable.Merge(newtable);

                        }
                        else
                        {
                            mytable = Sim.GetTable("B_Region", " and  LCode='"   dr["LCode"].ToString()   "' order by LCode");

                        }
                    }
                    else
                    {
                        //以后的子节点,只保留最后三位;
                        HighLCode = dr["LCode"].ToString().Substring(dr["LCode"].ToString().Length - 3);
                        NewLCode = FNewCode   HighLCode;


                        if (mytable != null)
                        {
                            newtable = Sim.GetTable("B_Region", " and  LCode='"   dr["LCode"].ToString()   "' order by LCode");
                            newtable.Rows[0]["LCode"] = NewLCode;
                            mytable.Merge(newtable);

                        }
                        else
                        {
                            mytable = Sim.GetTable("B_Region", " and  LCode='"   dr["LCode"].ToString()   "' order by LCode");
                        }
                    }
                }
                ///已经获取完成Datatable,保存操作
                try
                {
                    mytable.TableName = "B_Region";
                    //保存   
                    bool info = Sim.Update(mytable, ref errorinfo);
                    if (info)
                    {
                        ResNum = 1;
                    }
                    else
                    {
                        ResNum = 0;
                    }
                    Databinding();
                }
                catch (Exception)
                {
                    ResNum = 0;
                }
                ResNum = 1;
            }
            return ResNum;
        }

        ///// <summary>
        ///// 节点当前后面位数更新
        ///// </summary>
        ///// <param name="ChildNode">当前节点</param>
        ///// <returns></returns>
        //protected int MoveNodes(string ChildNode)
        //{
        //    int ResNum = 0;
        //    string sql = "select LCode from B_Region where  LCode  like '"   ChildNode.Substring(0, ChildNode.Length - 3)   "%' and LEN(LCode)>="   ChildNode.Length   " and LCode<>'"   ChildNode   "' and LCode>'"   ChildNode   "' order by Lcode";
        //    DataTable MyTable = new DataTable();
        //    MyTable = SQLHelper.SQLHelper.GetDataTable(sql);
        //    try
        //    {
        //        string FirstCode = MyTable.Rows[0][0].ToString();
        //        if (FirstCode.Substring(FirstCode.Length - 3, 3) != "001")//当节点需要操作时 才进行操作
        //        {
        //            foreach (DataRow dr in MyTable.Rows)
        //            {
        //                //string OldCode=dr[]
        //            }

        //        }
        //    }
        //    catch (Exception)
        //    {

        //        ResNum = 1;
        //    }

        //    return ResNum;
        //}

        /// <summary>
        /// 节点的移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trees_Move(object sender, TreeClass.ChangeMyArgs e)
        {
            int Num_First = MoveNodes(e.NameInfo, e.RegionName);
            if (Num_First == 0)
            {
                MessageBox.Show("操作失败,请刷新后重试");
            }
        }
        #endregion
    }
}