TreeView加载数据库,在线等待,十分感谢
我和朋友遇到一个问题,我水平有限,我写了两天,总是不成功,大家有会的帮我写下代码,感谢万分!有2个表(例)
a 表
id name parentid(父节点) depth(层次不确定)
11 中国 0 0
12 东北 11 1
13 华北 11 1
14 大连 12 2
15 北京 13 2
b 表
id name parentid (与上表id关联) parentname
1 小东 14 大连
2 小明 14 大连
3 小红 15 北京
大概要的效果如下
中国
--东北
---大连
----小东
----小明
--华北
---北京
----小红
如何在TreeView中动态加载 ,两个表不能够改或合并,而且因为a表层次不确定,想到用递归,水平有限,总写不好。帮我完成这个代码,不胜感激!
--------------------编程问答-------------------- 沙发,看楼下的 --------------------编程问答-------------------- 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);
}
} --------------------编程问答--------------------
DataSet data = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//data = 数据;
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=;database=pubs");
SqlDataAdapter da = new SqlDataAdapter("select * from parent_channel ", conn);
conn.Open();
da.Fill(data);
InitTree(TreeView1.Nodes, "0");
}
}
public void InitTree(TreeNodeCollection Nds, string parentID)
{
TreeNode newNode;
DataRow[] rows = data.Tables[0].Select("parentid=" + parentID);
foreach (DataRow row in rows)
{
newNode = new TreeNode(
row["title"].ToString(),
row["lid"].ToString(),
row["lid"].ToString());
Nds.Add(newNode);
InitTree(newNode.ChildNodes, row["lid"].ToString());
}
}
lid= 你的 id
parnetid = 你的 fid
title =你的 channelname
--------------------编程问答-------------------- 感谢大家的回复 可是这不是我想要的结果 我的是两个表 哪位可以帮我写一下 我写了2天了 感谢 --------------------编程问答-------------------- 帮你顶了,我的水平有限,而且你说的也不是很清楚!未能为力! --------------------编程问答-------------------- 那还不简单,我上面的那个递归中判断下如果他下面没有子节点,就查询你的第2个表,把节点ID去找符合条件的用户就行了啊
--------------------编程问答-------------------- SqlCommand com = new SqlCommand ("Select * from a 表; Select * from b 表",conn);
SqlDataAdapter da = new SqlDataAdapter(com);
da.Fill(ds);
for (int i=0 ; i< ds.Table[0].Rows.Count; i++)
{
TreeNode TN = new TreeNode (ds.Table[0].Rows[i]["name"].ToString(),ds.Table[0].Rows[i]["id"].ToString());
DataRow[] drA = ds.Table[0].Select("parentid = " + ds.Table[0].Rows[i]["id"].ToString());
for(int j=0; j<drA.Length; j++)
TN.ChildNodes.Add(new TreeNode (drA[j]["name"].ToString(),drA[j]["id"].ToString()))
DataRow[] drB = ds.Table[1].Select("parentid = " + ds.Table[0].Rows[i]["id"].ToString());
for(int j=0; j<drB.Length; j++)
TN.ChildNodes.Add(new TreeNode (drB[j]["name"].ToString(),drB[j]["id"].ToString()))
TreeView1.Nodes.Add(TN);
}
--------------------编程问答-------------------- 用下面的参考。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace zcgl
{
public partial class Frm_yggl : Form
{
//获取当前选定的节点值
public int CurrentVal = 0;
//获取控件树的深度
public int pubDeep = 0;
// 获取子树节点的个数
public int pubChildNum = 0;
//获取当前节点对象
public TreeNode CurrentNode = null;
//设置 对话框
public DialogResult dr;
public DataSet ds1 = null;
public DataSet ds2 = null;
public DataSet ds3 = null;
public Frm_yggl()
{
InitializeComponent();
}
private void Frm_yggl_Load(object sender, EventArgs e)
{
inittree();
}
private DataSet Bind(string sql)
{
string connectionInfo = (ConfigurationSettings.AppSettings["ConnectionString"]);
SqlConnection conn = new SqlConnection(connectionInfo);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
private void KK(string sql)
{
string connectionInfo = (ConfigurationSettings.AppSettings["ConnectionString"]);
SqlConnection conn = new SqlConnection(connectionInfo);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
}
protected void inittree()
{
tvShow.Nodes.Clear();
DataView dv = Bind("select *from dept").Tables[0].DefaultView;
ds1 = Bind("select * from dept2");
ds2 = Bind("select *from worker");
if (dv.Table.Rows.Count > 0)
{
foreach (DataRowView dr in dv)
{
TreeNode tn = new TreeNode();
tn.Text = dr[1].ToString();
tn.Tag = dr[0].ToString();
tvShow.Nodes.Add(tn);
childnode_1(tn);
}
}
}
protected void childnode_1(TreeNode tn)
{
int second = Int32.Parse(tn.Tag.ToString());
DataView dv = ds1.Tables[0].DefaultView;
if (dv.Table.Rows.Count > 0)
{
dv.RowFilter = "d_id=" + second;
if (dv.Count > 0)
{
foreach (DataRowView dr in dv)
{
TreeNode tn_2 = new TreeNode();
tn_2.Text = dr[1].ToString();
tn_2.Tag = dr[0].ToString();
tn.Nodes.Add(tn_2);
DiGui(ds2.Tables[0], tn_2);
}
}
}
}
private void DiGui(DataTable dt, TreeNode tn)
{
DataView dv = new DataView(dt);
dv.RowFilter = "deptNO=" + Int32.Parse(tn.Tag.ToString());
foreach (DataRowView drv in dv)
{
TreeNode tn_n = new TreeNode();
tn_n.Text = drv[1].ToString();
tn_n.Tag = drv[0].ToString();
tn.Nodes.Add(tn_n);
DiGui(dt, tn_n);
}
}
public void tvShow_AfterSelect(object sender, TreeViewEventArgs e)
{
string txt_str = e.Node.Text; // 这个语句可以取到节点的text
}
}
}
--------------------编程问答-------------------- 在查询的时候。
分为4个查询语句
用distinct函数来查询分支
--------------------编程问答-------------------- 我只做過兩層的,三層的沒寫過。
不過樓群上幾位代易做图都很有參考價值。。。 --------------------编程问答--------------------
嘿.嘿.. 楼主似乎是我们老乡..
代码我没试过,但应该是可以的.
--------------------编程问答-------------------- http://topic.csdn.net/u/20080424/16/1e9e6131-8e25-4212-9703-0e8d4465819c.html
DataTable tableA = null;
DataTable tableB = null;
void page_load(object sender, EventArgs e) {
if (!IsPostBack) {
tableA = //获得A表全部数据;
tableB = //获得B表全部数据;
DataRow[] rows = tableA.Select(string.Format("parentid='{0}'", 0));
foreach (DataRow row in rows) {
string id = row["id"].ToString();
TreeNode newNode = new TreeNode(row["name"].ToString(), id);
BuilderNode(newNode, id);
this.TreeView1.Nodes.Add(newNode);
}
}
}
void BuilderNode(TreeNode node,string id) {
DataRow[] rows = tableA.Select(string.Format("parentid='{0}'",id));
if (rows != null && rows.Length > 0) {
foreach (DataRow row in rows) {
string id = row["id"].ToString();
TreeNode newNode = new TreeNode(row["name"].ToString(), id);
BuilderNode(newNode, id);
node.ChildNodes.Add(newNode);
}
} else {
rows = tableB.Select(string.Format("parentid='{0}'", id));
foreach (DataRow row in rows) {
node.ChildNodes.Add(new TreeNode(row["name"].ToString(), row["id"].ToString()));
}
}
}
补充:.NET技术 , ASP.NET