asp.net TreeView 的使用

燕兒歸發表於2015-09-30
        protected void CreateNode()
        {
            DataTable d = new DataTable();
            using (SqlConnection sql = new SqlConnection(ConnectionString))
            {
                SqlCommand sq = new SqlCommand("select [name] as 表名,[type] from [sysobjects]", sql);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = sq;
                sda.Fill(d);
            }
            DataRow[] dr = d.Select("[type] = 'u'", "表名");
            if (dr.Length > 0)
            {
                foreach (DataRow drr in dr)
                {
                    TreeNode tn = new TreeNode();
                    tn.Value = drr["表名"].ToString();
                    tn.Text = drr["表名"].ToString();
                    tn.Expanded = false;
                    tn.SelectAction = TreeNodeSelectAction.Expand;
                    TreeView1.Nodes.Add(tn);
                    CreateChild(tn, dts);
                }
            }
            else
            {
                TreeNode t = new TreeNode();
                t.Value = "空";
                t.Text = "空";
                t.Expanded = false;
                t.SelectAction = TreeNodeSelectAction.Expand;
                TreeView1.Nodes.Add(t);
            }
        }

        protected void CreateChild(TreeNode tnn, DataTable dtt)
        {
            DataRow[] dr = dtt.Select("表名='" + tnn.Value + "'", "表名");
            if (dr.Length > 0)
            {
                foreach (DataRow drw in dr)
                {
                    TreeNode ts = new TreeNode();
                    ts.Value = drw["列名"].ToString();
                    ts.Text = drw["列名"].ToString() + "(" + drw["型別"].ToString() + "," + drw["長度"].ToString() + ")";
                    ts.SelectAction = TreeNodeSelectAction.Expand;
                    //ts.Expanded = false;
                    tnn.ChildNodes.Add(ts);
                    CreateChild(ts, dtt);
                }
            }
        }

        protected DataTable CreateTable()
        {
            DataTable d = new DataTable();
            using (SqlConnection sql = new SqlConnection(ConnectionString))
            {
                SqlCommand sq = new SqlCommand("select a.[name] as 表名 ,b.name as 列名,type_name(b.system_type_id)as 型別,b.max_length as 長度 from [sysobjects] a ,sys.columns b where a.[type] = 'u' and object_id=object_id(a.[name])", sql);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = sq;
                sda.Fill(d);
            }
            return d;
        }






相關文章