shangwd

C# treeview 绑定数据 【转】
private void bindTreeView1()
        {
            string sql = "select * from dm_category";
            DataTable dt = db.ExecuteDataTable(sql, CommandType.Text, null);
            DataRow[] dr = dt.Select("parent_id is null");
            for (int i = 0; i < dr.Length; i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = dr[i]["category_name"].ToString();
                tn.Tag = dr[i]["id"].ToString();
                    FillTree(tn, dt);
                treeView1.Nodes.Add(tn);
            }
        }
 
        private void FillTree(TreeNode node, DataTable dt)
        {
            DataRow[] drr = dt.Select("parent_id='" + node.Tag.ToString() + "'");
            if (drr.Length > 0)
            {
                for (int i = 0; i < drr.Length; i++)
                {
                    TreeNode tnn = new TreeNode();
                    tnn.Text = drr[i]["category_name"].ToString();
                    tnn.Tag = drr[i]["id"].ToString();
                    if (drr[i]["parent_id"].ToString() == node.Tag.ToString())
                    {
                        FillTree(tnn, dt);
                    }
                    node.Nodes.Add(tnn);
                }
            }
        }

  

posted on 2016-04-28 11:16  shangwd  阅读(4379)  评论(0编辑  收藏  举报