怎么用递归绑定数据库中的数据并显示在treeview中
2个回答
展开全部
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
this.tvMenu.Dispose();
GetDataToTable(); //填充数据方法
GetNode(this.tvMenu.Nodes, "0"); //使用递归方法动态添加节点
}
}
/// <summary>
/// DataSet读取数据
/// </summary>
private void GetDataToTable() {
string str = "server=.;database=MyOffice;uid=sa;pwd=sa2008";
SqlConnection conn = new SqlConnection(str);
string sql = "select * from SysFun";
SqlDataAdapter dd = new SqlDataAdapter(sql, conn);
dd.Fill(ds, "SysFun");
}
/// <summary>
/// 使用递归动态添加节点
/// </summary>
/// <param name="tnc"></param>
/// <param name="patentId"></param>
private void GetNode(TreeNodeCollection tnc, string patentId) {
DataView dv = new DataView();
dv.Table=ds.Tables["sysfun"];
TreeNode tn = null;
dv.RowFilter = "ParentNodeId=" + patentId;
foreach (DataRowView drv in dv)
{
tn = new TreeNode();
tn.Value = drv["NodeId"].ToString();
tn.Text = drv["DisplayName"].ToString();
tn.NavigateUrl = drv["NodeURL"].ToString();
tnc.Add(tn);
GetNode(tn.ChildNodes, tn.Value);
}
}
{
if (!IsPostBack) {
this.tvMenu.Dispose();
GetDataToTable(); //填充数据方法
GetNode(this.tvMenu.Nodes, "0"); //使用递归方法动态添加节点
}
}
/// <summary>
/// DataSet读取数据
/// </summary>
private void GetDataToTable() {
string str = "server=.;database=MyOffice;uid=sa;pwd=sa2008";
SqlConnection conn = new SqlConnection(str);
string sql = "select * from SysFun";
SqlDataAdapter dd = new SqlDataAdapter(sql, conn);
dd.Fill(ds, "SysFun");
}
/// <summary>
/// 使用递归动态添加节点
/// </summary>
/// <param name="tnc"></param>
/// <param name="patentId"></param>
private void GetNode(TreeNodeCollection tnc, string patentId) {
DataView dv = new DataView();
dv.Table=ds.Tables["sysfun"];
TreeNode tn = null;
dv.RowFilter = "ParentNodeId=" + patentId;
foreach (DataRowView drv in dv)
{
tn = new TreeNode();
tn.Value = drv["NodeId"].ToString();
tn.Text = drv["DisplayName"].ToString();
tn.NavigateUrl = drv["NodeURL"].ToString();
tnc.Add(tn);
GetNode(tn.ChildNodes, tn.Value);
}
}
2011-10-08
展开全部
先把数据全部取出来 放到datatable中 然后先绑父节点 根据父节点的名字往下递归绑子节点
追问
具体代码怎么写啊。
追答
public void loadInfo()
{
SqlDataReader Dr = Meter.getEMeterInfoFromMeter();
while (Dr.Read())
{
electMeterTreeView.Nodes.Add(Dr["meterName"].ToString());
electMeterTreeView.Nodes[i].Tag = Dr["meterId"].ToString();
addRelationNode(electMeterTreeView.Nodes[i++]);
}
Dr.Close();
}
private void addRelationNode(TreeNode tn)
{
int comId=CompanyMeter.getComIdByMeterId(Convert.ToInt32(tn.Tag));
string comname = CompanyMeter.getcompanychildnode(comId );
tn.Nodes.Add(comname.ToString());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询