如何在Treeview中动态创建和显示节点 [英] how to create and display nodes dynamically in treeview

查看:128
本文介绍了如何在Treeview中动态创建和显示节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生
我想在Treeview中动态添加节点.我也想将那些节点与数据库链接.这样,当我单击该节点时,它应该显示其文本,html,图像等数据.

问候
Mehsud

Sir
I want to Add nodes in Treeview Dynamically. Also I want to link those nodes with database. so that when i click that node, it should display data whether its text, html,image,etc.

Regards
Mehsud

推荐答案

//此示例是将数据绑定到treenode的一种方法.

class dataCls
{
public int Key {get;放; }
公共对象数据{get;放; }
}

类Sample
{
私有无效createTreeStructure()
{
//使用SqlDataAdapter或SqlDataReader的SQL查询.
//此示例将使用SqlDataReader

SqlConnection conn =新的SqlConnection(connectionstring);
SqlCommand cmd =新的SqlCommand(
从tblExample中选择cID,cName,cData",
conn);

SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read());
{
dataCls dataObj =新的dataCls();
dataObj.Key =(int)dr [0];
dataObj.Data = dr [2];
TreeNone n =新的TreeNode((string)dr [1]);
n.Tag = dataObj;
treeView1.Nodes.Add(n);
}
}
}
//This Sample is one way to bind data to a treenode.

class dataCls
{
public int Key { get; set; }
public object Data { get; set; }
}

class Sample
{
private void createTreeStructure()
{
// Your sql query using SqlDataAdapter or SqlDataReader.
// this example will use SqlDataReader

SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(
"select cID, cName, cData from tblExample",
conn);

SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read());
{
dataCls dataObj= new dataCls();
dataObj.Key = (int)dr[0];
dataObj.Data = dr[2];
TreeNone n = new TreeNode((string)dr[1]);
n.Tag = dataObj;
treeView1.Nodes.Add(n);
}
}
}


您可以看到此链接

单击

点击

点击
You can see this links

Click

Click

Click


这篇关于如何在Treeview中动态创建和显示节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆