treeview绑定?如何用C#的treeview动态绑定数据库
大家好,treeview绑定相信很多的网友都不是很明白,包括如何用C#的treeview动态绑定数据库也是一样,不过没有关系,接下来就来为大家分享关于treeview绑定和如何用C#的treeview动态绑定数据库的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!
如何用C#的treeview动态绑定数据库
asp.net 2.0中TREEVIEW中动态增加结点
在asp.net 2.0中,要动态从数据库中取出内容,动态增加结点,其实不难,比如以SQL SERVER 2000的PUBS数据库为例子,要以树型列表方式,取出作者,做为根结点,然后取出每位作者写过什么书,作为子结点,可以这样
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Configuration"%>
<!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamic Population of the TreeView Control</title>
<script runat=server>
void Node_Populate(object sender,
System.Web.UI.WebControls.TreeNodeEventArgs e)
{
if(e.Node.ChildNodes.Count== 0)
{
switch( e.Node.Depth)
{
case 0:
FillAuthors(e.Node);
break;
case 1:
FillTitlesForAuthors(e.Node);
break;
}
}
}
void FillAuthors(TreeNode node)
{
string connString= System.Configuration.ConfigurationSettings.
ConnectionStrings["NorthwindConnnection"].ConnectionString;
SqlConnection connection= new SqlConnection(connString);
SqlCommand command= new SqlCommand("Select* From authors",connection);
SqlDataAdapter adapter= new SqlDataAdapter(command);
DataSet authors= new DataSet();
adapter.Fill(authors);
if(authors.Tables.Count> 0)
{
foreach(DataRow row in authors.Tables[0].Rows)
{
TreeNode newNode= new
TreeNode(row["au_fname"].ToString()+""+
row["au_lname"].ToString(),
row["au_id"].ToString());
newNode.PopulateOnDemand= true;
newNode.SelectAction= TreeNodeSelectAction.Expand;
node.ChildNodes.Add(newNode);
}
}
}
void FillTitlesForAuthors(TreeNode node)
{
string authorID= node.Value;
string connString= System.Configuration.ConfigurationSettings.
ConnectionStrings["NorthwindConnnection"].ConnectionString;
SqlConnection connection= new SqlConnection(connString);
SqlCommand command= new SqlCommand("Select T.title,
T.title_id From titles T"+
" Inner Join titleauthor TA on
T.title_id= TA.title_id"+
" Where TA.au_id='"+ authorID+"'", connection);
SqlDataAdapter adapter= new SqlDataAdapter(command);
DataSet titlesForAuthors= new DataSet();
adapter.Fill(titlesForAuthors);
if(titlesForAuthors.Tables.Count> 0)
{
foreach(DataRow row in titlesForAuthors.Tables[0].Rows)
{
TreeNode newNode= new TreeNode(
row["title"].ToString(), row["title_id"].ToString());
newNode.PopulateOnDemand= false;
newNode.SelectAction= TreeNodeSelectAction.None;
node.ChildNodes.Add(newNode);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeViewRunat="Server" ExpandImageUrl="Images/closed.gif"
CollapseImageUrl="Images/open.gif"
OnTreeNodePopulate="Node_Populate" ID="tvwauthors">
<Nodes>
<asp:TreeNodeText="Authors" PopulateOnDemand=true
Value="0"/>
</Nodes>
</asp:TreeView>
</div>
</form>
</body>
</html>
其中,注意ontreenodepopulate事件,是在展开树的结点时发生的,这里定义了自定义的NODE_POPULATE,在node_populate中,检查当前结点的深度,如果是0,就是根结点,于是就调用FillAuthors过程,取出所有的作者,如果深度是1,则是叶子结点,调用FillTitlesForAuthors过程。其中,要注意它们中的动态建立树结点的过程,如:
TreeNode newNode= new TreeNode(row["au_fname"].ToString()+""+
row["au_lname"].ToString(),
row["au_id"].ToString());
newNode.PopulateOnDemand= true;
newNode.SelectAction= TreeNodeSelectAction.Expand;
node.ChildNodes.Add(newNode);
其中, popluateondemand属性表明,该结点会动态扩展。
vb.net中treeview的数据绑定
第一步:定义两个DataTable对象,并把数据库中的两个表分别读入这两个DataTable对象中;如Dt1用于存储省名表,Dt2用于存储省辖市名表。
第二步:先绑定Dt1,以循环方式向TreeView添加Node。其中,Node.index就是Dt1中id字段的值,Node.Text就是Dt1中name字段的值。
第三步:绑定Dt2,同样以循环方式向TreeView添加Node。这里稍微有点复杂,需先判断下已经存在于TreeView中的节点,如果节点的索引是3,则向这个节点下添加id为3xx的子节点。
获得当前节点的索引值的语句是:dim s as Integer= TreeView1.Nodes(i).Nodes.IndexOf(Node)
为当前节点添加子节点的语句是:TreeView1.Nodes(i).Nodes(s).Nodes.Add(Node)
最后的效果是:TreeView中有北京、上海、山东等节点,点击"山东"则会展开其下面的"济南"、"青岛"、"烟台"等子节点。
TreeView怎样无限递归绑定数据(用C#语言写),谢谢。。
我把我以前用的表结构先介绍下
表主要用来保存权限分类的
字段 PopId权限ID主键
PopName权限名字
PostUrl链接到的URL
flid父节点ID
其中根节点的flid为0其他的父节点根据情况填写
页面实现的时候直接添加treeview控件
cs代码如下
protected void Page_Load(object sender, EventArgs e)
{
bindtree(PopId);
}
private void bindtree()
{
TreeView1.Nodes.Clear();
AddTree(0,(TreeNode)null);
}
public void AddTree(int ParentID, TreeNode pNode)
{
DataTable dt= new DataTable();
//这个是我获取数据源的代码
dt= logbll.BindPop();
DataView dvTree= new DataView(dt);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter="flid="+ ParentID;
foreach(DataRowView Row in dvTree)
{
TreeNode Node= new TreeNode();
if(pNode== null)
{//添加根节点
Node.Text= Row["PopName"].ToString();
TreeView1.Nodes.Add(Node);
//Node.Expanded= true;
AddTree(Int32.Parse(Row["PopId"].ToString()), Node);//再次递归
}
else
{//添加当前节点的子节点
Node.Text= Row["PopName"].ToString();
Node.NavigateUrl= Row["PostUrl"].ToString();
pNode.ChildNodes.Add(Node);
//Node.Expanded= true;
AddTree(Int32.Parse(Row["PopId"].ToString()), Node);//再次递归
}
}
}
你可以根据你的表来修改相应的字段名,下班了,没时间帮你写完整的了,代码能用,有什么问题明天再解决吧
OK,本文到此结束,希望对大家有所帮助。