TreeView控件操作开发篇之(四)

读取树节点从DataTable
 #region 读取树节点从DataTable

        /// <summary>
        ///读取树节点从DataTable 
        /// </summary>
        /// <param name="Tv">TreeView控件</param>
        /// <param name="Dt">要读取的DataTable</param>
        /// <param name="IsAppend">是否追加</param>
        /// <param name="ParentColumnIndex">父节点编号名称字段的索引</param>
        /// <param name="CurrColumnIndex">当前节点编号名称字段的索引</param>
        /// <param name="CurrNameColumnIndex">当前节点Text名称字段的索引</param>
        /// <returns></returns>
        public bool ReadNodeFromDataTable(TreeView Tv, DataTable Dt, bool IsAppend, int ParentColumnIndex, int CurrColumnIndex, int CurrNameColumnIndex)
        {
            try
            {
                if (!IsAppend)
                {
                    Tv.Nodes.Clear();
                }

                if (Dt != null && Dt.Rows.Count > 0)
                {
                    DataRow[] Dr = null;
                    //找出所有的父节点编号为空或null的记录,也就是所有的父节点
                    Dr = Dt.Select(Dt.Columns[ParentColumnIndex].ColumnName + "=''or " + Dt.Columns[ParentColumnIndex].ColumnName + " is null");
                    //遍历父节点
                    for (int i = 0; i < Dr.Length; i++)
                    {
                        TreeNode pNode = new TreeNode(Dr[i][Dt.Columns[CurrNameColumnIndex].ColumnName].ToString());
                        pNode.Tag = Dr[i][Dt.Columns[CurrColumnIndex].ColumnName].ToString();//当前节点编号保存到节点的tag属性中
                        Tv.Nodes.Add(pNode);
                    }
                    for (int i = 0; i < Tv.Nodes.Count; i++)
                    {
                        forReadNodeFromDataTable(Tv.Nodes[i], Dt, ParentColumnIndex, CurrColumnIndex, CurrNameColumnIndex);
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch 
            {
                return false;
            }
        }

        /// <summary>
        /// 从DataTable中递归遍历树节点
        /// </summary>
        /// <param name="Node">树节点</param>
        /// <param name="Dt">DataTable</param>
        /// <param name="ParentColumnIndex">父节点编号名称字段的索引</param>
        /// <param name="CurrColumnIndex">当前节点编号名称字段的索引</param>
        /// <param name="CurrNameColumnIndex">当前节点Text名称字段的索引</param>
        private void forReadNodeFromDataTable(TreeNode Node,DataTable Dt,int ParentColumnIndex,int CurrColumnIndex,int CurrNameColumnIndex)
        {
            DataRow[] Dr = null;
            string tempTag = Node.Tag.ToString();//取出当前节点编号
            Dr = Dt.Select(Dt.Columns[ParentColumnIndex].ColumnName + "='" + tempTag + "'");//得到当前节点下的所有子节点
            for (int i = 0; i < Dr.Length; i++)
            {
                TreeNode cNode = new TreeNode(Dr[i][Dt.Columns[CurrNameColumnIndex].ColumnName].ToString());
                cNode.Tag = Dr[i][Dt.Columns[CurrColumnIndex].ColumnName].ToString();
                Node.Nodes.Add(cNode);
            }
            foreach (TreeNode aNode in Node.Nodes)
            {
                forReadNodeFromDataTable(aNode, Dt, ParentColumnIndex, CurrColumnIndex, CurrNameColumnIndex);
            }

        }


        #endregion

你可能感兴趣的:(tag,treeview)