急!急!急!C#代码
怎样将数据库中的数据动态加载到Treeview控件中
[解决办法]
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()); } } }}
[解决办法]
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); } }