日期:2014-05-19  浏览次数:20508 次

关于TreeView小问题!!在线等80分
ASP.net1.1     我想要一个TreeView完整的代码;从数据库中读出数据然后自动倒入到TreeView控件,谢谢

------解决方案--------------------
看这里能不能满足你的要求
http://blog.sina.com.cn/u/4acd6e560100070s
------解决方案--------------------
void creat_node(string
name,Microsoft.Web.UI.WebControls.TreeNode t1,string number)
{
//定义一个新的结点

Microsoft.Web.UI.WebControls.TreeNode t2=new
Microsoft.Web.UI.WebControls.TreeNode();
t2.ID=number;
t2.Text=name;

//判断是否为根结点
TreeView1.ExpandedImageUrl= "images/Depart03.bmp ";
if(t1.ID.Trim()== "### ")
{
TreeView1.Nodes.Add(t2);
t2.ImageUrl= "images/Depart01.bmp ";
}
else
{
t1.Nodes.Add(t2);
t2.ImageUrl= "images/Depart01.bmp ";
t1.ImageUrl= "images/Depart02.bmp ";
}

//取得这个结点的所有子结点,利用递归自动生成
string mySelectQuery = "SELECT ParentID, ID,Name FROM table1
where ParentID= ' " + number + " ' ";
string MyConnString= "provider=Microsoft.Jet.OLEDB.4.0;Data
Source= "+Server.MapPath( ". ")+ "/dt/mydata.mdb ";
OleDbConnection myConnection = new
OleDbConnection(MyConnString);
OleDbCommand myCommand = new
OleDbCommand(mySelectQuery,myConnection);
myConnection.Open();
OleDbDataReader myReader;
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
creat_node(myReader.GetString(2),t2,myReader.GetString(1));
}
myReader.Close();
myConnection.Close();
}
private void CreateProviderTreeBtn_Click(object sender,
System.EventArgs e)
{
Microsoft.Web.UI.WebControls.TreeNode t1=new
Microsoft.Web.UI.WebControls.TreeNode();
creat_node( " ",t1, "### ");
7、怎样使用TreeView???

//初始化TreeView数据
private void InitTV(int intVOrderLen)
{
SqlDataReader dr;
SqlCommand myCommand;
string strSel = "select * from bas_enterprise where
len(vieworder)= "+intVOrderLen+ " order by vieworder ";
myCommand = new SqlCommand (strSel, myConnection);
dr=myCommand.ExecuteReader();
while (dr.Read ())
{
TreeNode pNode=new TreeNode ();
pNode.Text =dr[ "EntpName "].ToString ();
pNode.ID =dr[ "ViewOrder "].ToString ();
TreeView1.Nodes .Add (pNode);

}
dr.Close ();

}
private void TreeView1_SelectedIndexChange(object sender,
Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)
{
TreeNode NewNode=TreeView1.GetNodeFromIndex
(TreeView1.SelectedNodeIndex);
string strNodeId=NewNode.ID .ToString ();
SqlDataReader dr;
SqlCommand myCommand;
int intVOrderLen=strNodeId.Length+2;
string strSel = "select * from bas_enterprise where
len(vieworder)= "+intVOrderLen+ " and vieworder like '% "+strNodeId+ "__% '
order by vieworder ";
myCommand = new SqlCommand (strSel, myConnection);
dr=myCommand.ExecuteReader();
NewNode.Nodes .Clear();
while (dr.Read ())
{
TreeNode pNode=new TreeNode ();
pNode.Text =dr[ "EntpName "].ToString ();
pNode.ID =dr[ "ViewOrder "].ToString ();
NewNode.Nodes.Add (pNode);
}
dr.Close ();
TreeView1.ExpandLevel=1;
}

------解决方案--------------------
对你有帮助
http://community.csdn.net/Expert/topic/5387/5387434.xml?temp=.8401758
-----