首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

急C#代码,该如何解决

2012-02-02 
急!急!急!C#代码怎样将数据库中的数据动态加载到Treeview控件中[解决办法]C# codevoid CreateTree(TreeNod

急!急!急!C#代码
怎样将数据库中的数据动态加载到Treeview控件中

[解决办法]

C# code
void CreateTree(TreeNode pnode, DataView dvm, string pid)        {            dvm.RowFilter = "ArchivesType_PID = '" + pid + "'";            foreach (DataRowView drview in dvm)            {                TreeNode node = new TreeNode();                node.Text = drview["ArchivesType_TypeName"].ToString();                node.Value = drview["ArchivesType_ID"].ToString();                //node.NavigateUrl = "rightlist.aspx?id=" + node.Value + "&depth=" + node.Depth + "&foot="+node.ChildNodes.Count+"";                //node.Target = "mainFrame";                pnode.ChildNodes.Add(node);                CreateTree(node, dvm, drview["ArchivesType_ID"].ToString());            }        }void CreateData()        {            string where = "";            //DataTable dt = ArchivesMana.GetList(where, param.ToArray());            DataTable dt = ArchivesMana.GetQZList(where, param.ToArray());            if (dt.Rows.Count > 0)            {                for (int i = 0; i < dt.Rows.Count; i++)                {                    TreeNode tn = new TreeNode();                    tn.Text = dt.Rows[i]["QZNAME"].ToString();                    tn.Value = dt.Rows[i]["qzid"].ToString();                    //tn.NavigateUrl = "rightlist.aspx?id=" + tn.Value + "&depth=" + tn.Depth + "&foot="+tn.ChildNodes.Count+"";                    //tn.Target = "mainFrame";                                        tree.Nodes.Add(tn);                    DataTable dtt = ArchivesMana.GetList(where, param.ToArray());                    if (dtt.Rows.Count != 0)                    {                        CreateTree(tree.Nodes[i], dtt.DefaultView, dt.Rows[i]["qzid"].ToString());                    }                }            }}
[解决办法]
C# code
private   void   BindTree() { DataTable   dt   =   数据源;   //自己去指定。。。 DataView   dv   =   new   DataView(dt); dv.RowFilter   =   "ParentID   is   null "; foreach   (DataRowView   drv   in   dv) { TreeNode   node   =   new   TreeNode(); node.Text   =   drv[ "Name "].ToString(); node.Value   =   drv[ "ID "].ToString(); node.ImageUrl   =   "~/images/folder.GIF "; node.Expanded   =   true; this.tvRight.Nodes.Add(node); AddReplies(dt,node); } } //递归函数 private   void   AddReplies(DataTable   dt,   TreeNode   node) { DataView   dv   =   new   DataView(dt); dv.RowFilter   =   "ParentID= ' "   +   node.Value   +   " ' "; foreach   (DataRowView   row   in   dv) { TreeNode   replyNode   =   new   TreeNode(); replyNode.Text   =   row[ "Name "].ToString(); replyNode.Value   =   row[ "ID "].ToString(); replyNode.Expanded   =   false; node.ChildNodes.Add(replyNode); AddReplies(dt,replyNode); } } 

热点排行