不会吧,windows应用程序的TreeView我搞得比较多,
现在家休息,
后天上班,如果楼主没有解决问题而且程序是用C#写的,
我可以帮你看看。[email protected]

解决方案 »

  1.   

    楼主给的那个链接是用VB.NET写的,C#好像有比较大的区别吧?
      

  2.   

    我想看C#的,有这样的文章吗?
    正在实现这个功能,谢谢.
    [email protected]
      

  3.   

    第一次点节点时把tag的值设置一下,第二次点的时候再判断一下,就不会出现重复加载节点的问题了。
      

  4.   

    把tag设置成什么?
    那又得判断什么呢?
      

  5.   

    你的代码我帮你改了一下,不过的代码写的感觉不是很清晰。
    修改如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.OleDb;namespace chuna
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TreeView treeView1;
    private System.ComponentModel.Container components = null;
    private const string ConnectString = @"server=localhost;uid=sa;pwd=;database=cn"; private SqlConnection CN;//=new SqlConnection(ConnectString);
    private SqlCommand sqlCmd;//= new SqlCommand(ConnectString);
    //DataSet ds=new DataSet();  public Form1()
    {
    InitializeComponent();
    this.CN = new SqlConnection(ConnectString);
    this.sqlCmd = new SqlCommand();
    this.sqlCmd.Connection = this.CN;
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.treeView1 = new System.Windows.Forms.TreeView();
    this.SuspendLayout();
    // 
    // treeView1
    // 
    this.treeView1.ImageIndex = -1;
    this.treeView1.Location = new System.Drawing.Point(32, 20);
    this.treeView1.Name = "treeView1";
    this.treeView1.SelectedImageIndex = -1;
    this.treeView1.Size = new System.Drawing.Size(424, 328);
    this.treeView1.TabIndex = 1;
    this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect_1);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(544, 365);
    this.Controls.Add(this.treeView1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    public static void Main() 
    {

    Application.Run(new Form1()); }


            



    public void Form1_Load(object sender, System.EventArgs e)
    {

    try 
    {
    CN.Open();
    SqlCommand sqlCmd= new SqlCommand();
    sqlCmd.Connection = CN;
    sqlCmd.CommandText = "SELECT id,zbname FROM zbroot";
    sqlCmd.CommandType = CommandType.Text ;
    //SqlDataAdapter adp = new SqlDataAdapter(sqlCmd.CommandText,CN);

    //adp.Fill(ds);

    SqlDataReader mysqlreader =sqlCmd.ExecuteReader();

    treeView1.Nodes.Clear();
    while (mysqlreader.Read()) 
    {

        
    TreeNode tree_root=new TreeNode();
    tree_root.Tag = mysqlreader.GetValue(0); //把编号放入tag中
    tree_root.Text = mysqlreader.GetString(1);//树上显示的是根节点名称 treeView1.Nodes.Add(tree_root); //请根据你数据库字段的类型来决定是否用getstring或其 }     }
    catch (Exception ex)
    {
    //MessageBox.Show(ex.ToString, "数据表根节点载入错误");
    throw(ex);
    }
    finally
    {
    CN.Close(); 
    }
    treeView1.ExpandAll();
    treeView1.Select();
    }
    public byte NodeLevel (TreeNode n )
    { //* 找出树中当前节点的级数
               byte i=1;
       //string m;
    while(  n.Parent != null)
    {
    n = n.Parent;
    i += 1; }
    return i;
    }

    private void treeView1_AfterSelect_1(object sender, System.Windows.Forms.TreeViewEventArgs e)
    {


    switch(NodeLevel(e.Node).ToString()) 
    {
    case "1":
    if (e.Node.GetNodeCount(false) == 0)
    {
    sqlCmd.CommandText = "select leafid,nianf from zbleaf where id ='" + e.Node.Tag.ToString() + "'";
    fill_treeleaf();
    }
    break;
    case "2":
    if (e.Node.GetNodeCount(false) == 0)
    {
    sqlCmd.CommandText = "select leaf2id,yuef from zbleaf2 where leafid ='" + e.Node.Tag.ToString() + "'";
    fill_treeleaf();
    }
    break; }

       
    } public  void fill_treeleaf()
    {   sqlCmd.Connection = CN;
    try
    {
                  
    CN.Open();
                    SqlDataReader mysqlreader =sqlCmd.ExecuteReader();
                    
    while (mysqlreader.Read())
    {
    TreeNode tree_leaf=new TreeNode();
    tree_leaf.Tag = mysqlreader.GetValue(0);
    tree_leaf.Text = mysqlreader.GetString(1);
    treeView1.SelectedNode.Nodes.Add(tree_leaf);
    }

    catch (Exception ex)
    {
    throw(ex);
    }
    finally
        {
    CN.Close();
        }
    } }
    }
      

  6.   

    晕了vb.net语法和c#搞错了,vb.net if..then..整个代码段也就是c#中的{}
    哎~~~
    太粗心了再次谢谢您
    你的代码写的很规范
    前面都空四个字符
    还得继续学习!