在form环境
数据库:
id       mc(名称)
0001  打印纸
0002   剪刀
0003     笔
0004     笔芯
0005     电池要实现treeView:样子 全部
   ┝打印纸
   ┝剪刀
   ┝笔
   ┝笔芯
   ┕电池
  
点击打印纸可以传值“0001”过去

解决方案 »

  1.   

    取数据到 _DataTable里
     TreeNode _AllNode = new TreeNode("全部");            for (int i = 0; i != _DataTable.Rows.Count; i++)
                {
                    TreeNode _Node = new TreeNode();
                    _Node.Text = _DataTable.Rows[i]["mc"].ToString();
                    _Node.tag = _DataTable.Rows[i]["id"].ToString();                _AllNode.Nodes.Add(_Node);
                }            treeView1.Nodes.Add(_AllNode);获取数据的时候 treeView1.SelectedNode.Tag;
      

  2.   

    /// <summary>
            /// Loads the tree view.
            /// </summary>
            /// <param name="oTreeView">The o tree view.</param>
            /// <param name="TitleText">The title text.</param>
            /// <param name="TextField">The text field.</param>
            /// <param name="TextFormatString">The text format string.</param>
            /// <param name="SwitchLocale">if set to <c>true</c> [switch locale].</param>
            /// <param name="NavigateUrl">The navigate URL.</param>
            /// <param name="Target">The target.</param>
            /// <param name="sql">The SQL.</param>
            public static void LoadTreeView(ref TreeView oTreeView, string TitleText, string[] TextField, string TextFormatString, bool SwitchLocale, string NavigateUrl, string Target, string sql)
            {
                int i = 0, j = 0;
                Guid ParentID = Guid.Empty;            if (SwitchLocale)
                {
                    //TextField = GetSwitchLocale(TextField);
                }
                //TreeNode oTNode = new TreeNode();
                //oTNode.Value = "LUAutumnAidStudy";
                //oTNode.Text = TitleText;            Hashtable tblNodes = new Hashtable(); // build a list of Nodes, Key=LUAASId, Value=Index
                TreeNode oNode;
                string[] orderBy = { "FullName" };
                LUAutumnAidStudyCollection source;
                if (sql != string.Empty)
                {
                    source = LUAutumnAidStudy.LoadCollection(sql, orderBy, true);
                }
                else
                {
                    source = LUAutumnAidStudy.LoadCollection(orderBy, true);
                }            // loop throught the Class table to build the Nodes and the List
                TreeNode[] oNodes = new TreeNode[source.Count]; // build an array of Nodes to hold all LUAutumnAidStudy
                foreach (LUAutumnAidStudy item in source)
                {
                    oNode = new TreeNode();
                    oNode.Value = item.LUAASId.ToString();
                    oNode.Text = GetFormatedText(item, TextField, TextFormatString);
                    oNode.NavigateUrl = String.Format(NavigateUrl, item.LUAASId.ToString());
                    oNode.Target = Target;
                    oNode.SelectAction = TreeNodeSelectAction.Expand;
                    oNodes[i] = oNode;
                    tblNodes.Add(item.LUAASId.ToString(), i);
                    ++i;
                }            // loop throught the Class table again to put the Nodes into proper hirearchy position
                foreach (LUAutumnAidStudy item in source)
                {
                    i = (int)tblNodes[item.LUAASId.ToString()];
                    ParentID = Guid.Empty;                if (tblNodes[ParentID.ToString()] == null)
                    {
                        j = 0;
                    }
                    else
                    {
                        j = (int)tblNodes[ParentID.ToString()];
                    }                if (ParentID != Guid.Empty)
                    {
                        oNodes[j].ChildNodes.Add(oNodes[i]);
                    }
                }            //loop throught the Class table again to put the Top Nodes into TreeView
                foreach (LUAutumnAidStudy item in source)
                {
                    i = (int)tblNodes[item.LUAASId.ToString()];
                    ParentID = Guid.Empty;                if (ParentID == Guid.Empty)
                    {
                        if (oNodes[i] != null)
                        {
                            oTreeView.Nodes.Add(oNodes[i]);
                        }
                    }
                }
                //oTreeView.Nodes.Add(oTNode);;
            }
      

  3.   


    /// <summary>
            /// Loads the tree view.
            /// </summary>
            /// <param name="oTreeView">The o tree view.</param>
            /// <param name="TitleText">The title text.</param>
            /// <param name="TextField">The text field.</param>
            /// <param name="TextFormatString">The text format string.</param>
            /// <param name="SwitchLocale">if set to <c>true</c> [switch locale].</param>
            /// <param name="NavigateUrl">The navigate URL.</param>
            /// <param name="Target">The target.</param>
            /// <param name="sql">The SQL.</param>
            public static void LoadTreeView(ref TreeView oTreeView, string TitleText, string[] TextField, string TextFormatString, bool SwitchLocale, string NavigateUrl, string Target, string sql)
            {
                int i = 0, j = 0;
                Guid ParentID = Guid.Empty;            if (SwitchLocale)
                {
                    //TextField = GetSwitchLocale(TextField);
                }
                //TreeNode oTNode = new TreeNode();
                //oTNode.Value = "LUAutumnAidStudy";
                //oTNode.Text = TitleText;            Hashtable tblNodes = new Hashtable(); // build a list of Nodes, Key=LUAASId, Value=Index
                TreeNode oNode;
                string[] orderBy = { "FullName" };
                LUAutumnAidStudyCollection source;
                if (sql != string.Empty)
                {
                    source = LUAutumnAidStudy.LoadCollection(sql, orderBy, true);
                }
                else
                {
                    source = LUAutumnAidStudy.LoadCollection(orderBy, true);
                }            // loop throught the Class table to build the Nodes and the List
                TreeNode[] oNodes = new TreeNode[source.Count]; // build an array of Nodes to hold all LUAutumnAidStudy
                foreach (LUAutumnAidStudy item in source)
                {
                    oNode = new TreeNode();
                    oNode.Value = item.LUAASId.ToString();
                    oNode.Text = GetFormatedText(item, TextField, TextFormatString);
                    oNode.NavigateUrl = String.Format(NavigateUrl, item.LUAASId.ToString());
                    oNode.Target = Target;
                    oNode.SelectAction = TreeNodeSelectAction.Expand;
                    oNodes[i] = oNode;
                    tblNodes.Add(item.LUAASId.ToString(), i);
                    ++i;
                }            // loop throught the Class table again to put the Nodes into proper hirearchy position
                foreach (LUAutumnAidStudy item in source)
                {
                    i = (int)tblNodes[item.LUAASId.ToString()];
                    ParentID = Guid.Empty;                if (tblNodes[ParentID.ToString()] == null)
                    {
                        j = 0;
                    }
                    else
                    {
                        j = (int)tblNodes[ParentID.ToString()];
                    }                if (ParentID != Guid.Empty)
                    {
                        oNodes[j].ChildNodes.Add(oNodes[i]);
                    }
                }            //loop throught the Class table again to put the Top Nodes into TreeView
                foreach (LUAutumnAidStudy item in source)
                {
                    i = (int)tblNodes[item.LUAASId.ToString()];
                    ParentID = Guid.Empty;                if (ParentID == Guid.Empty)
                    {
                        if (oNodes[i] != null)
                        {
                            oTreeView.Nodes.Add(oNodes[i]);
                        }
                    }
                }
                //oTreeView.Nodes.Add(oTNode);;
            }
      

  4.   


            /// <summary>
            /// Gets the formated text.
            /// </summary>
            /// <param name="target">The target.</param>
            /// <param name="textField">The text field.</param>
            /// <param name="textFormatString">The text format string.</param>
            /// <returns></returns>
            private static string GetFormatedText(LUAutumnAidStudy target, string[] textField, string textFormatString)
            {
                for (int i = 0; i < textField.Length; i++)
                {
                    PropertyInfo pi = target.GetType().GetProperty(textField[i]);
                    textFormatString = textFormatString.Replace("{" + i.ToString() + "}", (string)pi.GetValue(target, null));
                }
                return textFormatString;
            }
      

  5.   

    将数据取出来,然后生成树节点。
      //添加树的父节点
            private void AddParent(string str)
            {
                TreeNode tmp;
                tmp = new TreeNode();
                tmp.ImageIndex = 0;
                tmp.SelectedImageIndex = 0;
                tmp.Text = str;
                this.treeView_logdate.Nodes.Add(tmp);
                JoinParameter.tNFisrt = tmp;
            }
      //添加树的子节点
            private void AddChildNode(TreeNode parentTN, string str)
            {
                TreeNode tmp;
                tmp = new TreeNode();
                tmp.ImageIndex = 2;
                tmp.SelectedImageIndex = 2;
                tmp.Text = str;
    //          tmp.tag=对象    这里可以将一个对象绑定到某一个树节点下,取值的时候再强制转化一样
    //就可以得到一个对象
                parentTN.Nodes.Add(tmp);        }
     //联系几个参数的类
        public class JoinParameter
        {
            public static TreeNode tNFisrt = null;
            public static TreeNode tNSecond = null;
        }