treeview如何显示多表中的数据?

must_succeed 2003-08-29 11:00:31
treeview如何显示多表中的数据?这几个表的ID号是有关系的,例如:table1中的ID为两位数据的字段,另一字段为Name;table2中的ID为四位数据的字段,另一字段为Name;table3中的ID为六位数据的字段,另一字段为Name;table4中的ID为八位数据的字段,另一字段为Name;数值为:
table1
ID Name
11 How
table2
ID Name
1122 Are
table3
ID Name
112233 You
table4
ID Name
11223344 Ok
要求在treeview中显示Name的值,输出应为:
How
|
Are
|
You
|
Ok
如何去实现?谢谢!
...全文
83 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2003-09-01
  • 打赏
  • 举报
回复
I don't have time to go through previous solutions, if there is a conflict, ignore my solution

========
have you tried the solutions suggested by other people? since my method is not necessarily better

1. get four tables into a DataSet

2. create TreeNodes dynamically, the following is just an illustration, you should do a recursion instead

TreeView tvTree;
foreach (DataRow dr1 in DataSet1.Tables["table1"].Rows)
{
string sID = (string)dr["ID"];
TreeNode tvFirst = new TreeNode();
tvFirst.Text = (string)dr["Name"];
string sChild = String.Format("SUBSTRING(ID,1,{0}) = '{1}'", sID.Length, sID);

foreach (DataRow dr2 in DataSet1.Tables["table2"].Select(sChild))
{
string sID2 = (string)dr2["ID"];
TreeNode tvSecond = new TreeNode();
tvSecond.Text = (string)dr2["Name"];
string sChild2 = String.Format("SUBSTRING(ID,1,{0}) = '{1}'", sID2.Length, sID2);

//.....repeat for the other tables
tvFirst.Nodes.Add(tvSecond);
}
tvTree.Nodes.Add(tvFirst);
}
雪狼1234567 2003-08-30
  • 打赏
  • 举报
回复
首先取得数据

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from tree",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"tree");
public void CreateNodesOfParent(string iParent,TreeNode pNode)
{


DataView dvwData = new DataView(this.dt.Tables[0]);
if (iParent == null)
{
dvwData.RowFilter = "[ParentID] is null";
}
else
{
dvwData.RowFilter = "[ParentID] = " +"'"+ iParent+"'";
}


foreach(DataRowView Row in dvwData)
{
// Call Stack !
if(pNode == null)
{
TreeNode zNode = this.treeView1.Nodes.Add(Row["Name"].ToString());
zNode.Tag = Row["ID"].ToString();
CreateNodesOfParent((Row["ID"].ToString()),zNode);
}
else
{
TreeNode zNode = pNode.Nodes.Add(Row["Name"].ToString());
zNode.Tag = Row["ID"].ToString();
CreateNodesOfParent((Row["ID"].ToString()),zNode);
}

}
}
public void IniTree()
{
this.treeView1.BeginUpdate();
CreateNodesOfParent(root,null);
this.treeView1.ExpandAll();
this.treeView1.EndUpdate();
}
雪狼1234567 2003-08-30
  • 打赏
  • 举报
回复
你不必要建那么多表,如果有10层的话,难道你要建10个表吗?
所以数据库设计有问题,一般遇到这种情况用如下的表结构:
create table Tree(
ID varchar(30),
name Varcar(50),
ParentID varchar(30))
你要面的情况就是如此
ID name ParenID
11 How root
1122 Are 11
112233 You 1122
11223344 Ok 112233
基于以上的表结构,最后放在树结点里的代码如下:


jiezhi 2003-08-30
  • 打赏
  • 举报
回复
不管數據來自多少個物理表,只要得到構造樹的DataTable什么的就行,下面就可以使用遞歸來生成tree。
大户翁 2003-08-30
  • 打赏
  • 举报
回复
在treeview 根节点 鼠标点击是 动态查询数据生成节点(同时在其tag中标明
对应表 便宜查询表数据生成其节点)
Brunhild 2003-08-30
  • 打赏
  • 举报
回复
就你的字面意思应该不难啊:

private System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection("...");

private void button1_Click(object sender, System.EventArgs e)
{
this.LoadData("", this.treeView1.Nodes);
}

private void LoadData(string parentid, System.Windows.Forms.TreeNodeCollection nodes)
{
string tablename;
switch(parentid.Length)
{
case 2:
tablename="table2";
break;
case 4:
tablename="table3";
break;
case 6:
tablename="table4";
break;
default:
tablename="table1";
break;
}
string sql="select id, name from " +tablename;
if (parentid.Length>0)
sql+=string.Format(" where substring(id,1, {0})='{1}'", parentid.Length, parentid);

System.Data.SqlClient.SqlDataAdapter adp=new System.Data.SqlClient.SqlDataAdapter(sql, cn);
System.Data.DataTable tbl=new System.Data.DataTable();
adp.Fill(tbl);
adp.Dispose();

System.Windows.Forms.TreeNode node;
for(int i=0;i<tbl.Rows.Count ;i++)
{
node=new System.Windows.Forms.TreeNode(tbl.Rows[i].ItemArray[1].ToString());
node.Tag =tbl.Rows[i].ItemArray[0];
nodes.Add(node);

LoadData(node.Tag.ToString() , node.Nodes );
}
tbl.Dispose();
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧