winform treeview 通過節點名稱添加子節點

來源:互聯網
上載者:User

標籤:des   winform   style   blog   color   os   io   資料   

/// <summary>        /// 新增人員節點(利用TreeNodeCollection)        /// </summary>        /// <param name="tnc">TreeNodeCollection(TreeView的節點集合)</param>        /// <param name="pid_val">父id的值</param>        /// <param name="id">資料庫 id 欄位名</param>        /// <param name="pid">資料庫 父id 欄位名</param>        /// <param name="text">資料庫 文本 欄位值</param>        public static void Bind_Leaves(DataTable dt, TreeView tnc, string id, string pid, string text,ContextMenuStrip contextMenuStrip1, ImageList imageList1)        {            DataView dv = new DataView(dt);//將DataTable存到DataView中,以便於篩選資料            TreeNode tn;//建立TreeView的節點(TreeNode),以便將取出的資料添加到節點中            foreach (DataRowView drv in dv)            {                tn = new TreeNode();//建立一個新節點(學名叫:一個執行個體)                tn.Name = drv[id].ToString();//節點的Value值,一般為資料庫的id值                tn.Text = drv[text].ToString();//節點的Text,節點的文本顯示                foreach (TreeNode node in tnc.Nodes)                {                    if (node.Name == drv[pid].ToString()){                        node.Nodes.Add(tn);                        tn.ContextMenuStrip = contextMenuStrip1;                                                tn.ImageIndex = tn.SelectedImageIndex = 1;                        break;                    }                                        }                //tnc.Nodes[drv[pid]].Nodes.Add(tn);//將該節點加入到TreeNodeCollection(節點集合)中                //Bind_Leaves(dt, tn.Nodes, tn.Name, id, pid, text);//遞迴(反覆調用這個方法,直到把資料取完為止)            }        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.