求treeview完美解决 不会吧,windows应用程序的TreeView我搞得比较多,现在家休息,后天上班,如果楼主没有解决问题而且程序是用C#写的,我可以帮你看看。[email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主给的那个链接是用VB.NET写的,C#好像有比较大的区别吧? 我想看C#的,有这样的文章吗?正在实现这个功能,谢谢.[email protected] 第一次点节点时把tag的值设置一下,第二次点的时候再判断一下,就不会出现重复加载节点的问题了。 把tag设置成什么?那又得判断什么呢? 你的代码我帮你改了一下,不过的代码写的感觉不是很清晰。修改如下: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(); } } }} 晕了vb.net语法和c#搞错了,vb.net if..then..整个代码段也就是c#中的{}哎~~~太粗心了再次谢谢您你的代码写的很规范前面都空四个字符还得继续学习! 求救,the underlying provider failed on open Repeater横向纵向显示数据。。。求解 请问下,如果根据对数求底 我也杯具了,10进制转不了ASCII 字符! 一个小问题 BalloonTipClicked事件不能被触发 50分求c#.net如何实现接收传真 大家 快帮帮我,怎么区分用户输入的是字符还是数字?? 无法在web服务器上启动调试。登录失败:未知的用户名或者密码错误 在C#中怎样用RSA加密数据? 日资企业的工作环境怎么样? 如何在一个事件中得到调用它的控件????
正在实现这个功能,谢谢.
[email protected]
那又得判断什么呢?
修改如下:
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();
}
} }
}
哎~~~
太粗心了再次谢谢您
你的代码写的很规范
前面都空四个字符
还得继续学习!