日期:2014-05-18  浏览次数:21097 次

winfrom里treeview怎么动态的添加节点
例如
中国
  江苏
  南京
  南通
  上海
  北京
比如点击
连接数据库动态生成。。

------解决方案--------------------
http://blog.csdn.net/wtpmcheng/article/details/5405863
------解决方案--------------------
C# code
    private void btnAddType_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode node = tvType.SelectedNode;
                AddProductCategory add = new AddProductCategory();
                if (add.ShowDialog() == DialogResult.OK)
                {
                    TreeNode nd = new TreeNode();
                    nd.Text = add.Name;
                    node.Tag =;
                    if (node == null)
                    {
                        tvType.Nodes.Add(nd);
                    }
                    else
                    {
                        node.Nodes.Add(nd);
                        tvType.SelectedNode = nd;
                    }
                }
            }
            catch (Exception ex)
            {
                FunctionsUI.CreateErrorDialog(ex.Message);
            }
        }

------解决方案--------------------
http://www.cnblogs.com/duhongyu216/articles/1702591.html
------解决方案--------------------
楼上的大神们的链接都很详细了 在给你一段
C# code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class App_page_NaviLeft : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string SchoolID = "";
            if (Session["SchoolID"] != null && Session["SchoolID"].ToString() != "")
            {
                SchoolID = Session["SchoolID"].ToString();
                this.TextBox_SchoolID.Text = SchoolID;
            }
            else
            {
                ShowMessage("Session已超时,请重新登录!");
                return;
            }
            //动态绑定DateList
            DatelistAdd();
            //只添加第一层的节点
            PopulateCategories();
        }
    }

    #region   动态绑定DateList
    private void DatelistAdd()
    {
        string strConn = ConfigurationManager.AppSettings["conn"].ToString();   // 获取连接数据库的字符串

        string strSQL = "Select * From Schoolname_IP where IsDelete='0' order by id desc";
        System.Data.SqlClient.SqlConnection sqlConn = new SqlConnection(strConn);
        System.Data.SqlClient.SqlCommand sqlComm = new SqlCommand(strSQL, sqlConn);
        try
        {
            sqlConn.Open();

            System.Data.SqlClient.SqlDataReader sqlDR = sqlComm.ExecuteReader();

            if (sqlDR != null)
            {
                while (sqlDR.Read())
                {
                    ListItem LI = new ListItem();
                    LI.Text = sqlDR["schoolname"].ToString();
                    LI.Value = sqlDR["id"].ToString();
                    this.DropDownList_School.Items.Add(LI);
                }
            }

            sqlDR.Close();
            this.DropDownList_School.SelectedValue = this.TextBox_SchoolID.Text;
        }
        catch
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.alert('获得数据的过程出现了错误!请检查数据库的连接是否正确!');</script>");
        }
        finally
        {
            sqlConn.Close();
        }
    }
    #