C#中怎样遍历TreeView中的所有节点? 递归void test(TreeNode node){ foreach(TreeNode n in node.Nodes) { //在这里读取n的内容 test(n); }} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 递归,并加入到treeView private void Form1_Load(object sender, System.EventArgs e) { this.sqlDataAdapter1.Fill(dataSet1); for (int i=0;i<dataSet1.Tables[0].Rows.Count;i++) { if ((dataSet1.Tables[0].Rows[i]["parentID"]==null) || (dataSet1.Tables[0].Rows[i]["parentID"].ToString()=="") ) { try { TreeNode tr=new TreeNode(); tr.Text=dataSet1.Tables[0].Rows[i]["DeptName"].ToString(); tr.Tag=dataSet1.Tables[0].Rows[i]["CompanyID"].ToString(); treeView1.Nodes.Add(tr); // MessageBox.Show(tr.Index.ToString()); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } else { SeekNode(treeView1.Nodes ,dataSet1.Tables[0].Rows[i]["ParentID"].ToString().Trim(),dataSet1.Tables[0].Rows[i]["DeptName"].ToString(),dataSet1.Tables[0].Rows[i]["CompanyID"].ToString()); } } treeView1.Nodes[0].Expand(); }/// <summary>/// 递归遍历TreeView/// </summary>/// <param name="tru"></param>/// <param name="s_parentID"></param>/// <param name="s_text"></param>/// <param name="s_tag"></param> private void SeekNode(System.Windows.Forms.TreeNodeCollection tru,string s_parentID ,string s_text,string s_tag) { // TreeNodeCollection tndco; //tndco=treeView1.Nodes; foreach ( TreeNode tnd in tru ) { //MessageBox.Show(tnd.Tag.ToString()); try { if ( tnd.Tag.ToString() == s_parentID ) { TreeNode tr=new TreeNode(); tr.Text= s_text; tr.Tag=s_tag; tnd.Nodes.Add(tr); return ; } else { SeekNode(tnd.Nodes,s_parentID,s_text,s_tag); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } Socket接受数据的问题 C#小程序,为什么限制语句不起作用?目的是让第一个字母必须为小写字母,后面必须是数字,字母或者下划线! C#菜鸟求助 如何编写一个选择题的程序? 帮忙分析个问题WebRequest datetimepicker问题 如何在c#中继承Graphics 关于正则表达式 做的程序关闭了但仍在任务管理器中存在,郁闷中 C#中怎样写ANSI格式的文本文件? 求 BBS 论坛 数据库内容的显示代码。。。50分。。不够再加 菜鸟问题:DesignerCategory 到底用来干嘛的??谁给个浅显的说法?? xml读写问题 急急急!!!!!!!!!
private void Form1_Load(object sender, System.EventArgs e)
{
this.sqlDataAdapter1.Fill(dataSet1);
for (int i=0;i<dataSet1.Tables[0].Rows.Count;i++)
{
if ((dataSet1.Tables[0].Rows[i]["parentID"]==null) || (dataSet1.Tables[0].Rows[i]["parentID"].ToString()=="") )
{
try
{ TreeNode tr=new TreeNode();
tr.Text=dataSet1.Tables[0].Rows[i]["DeptName"].ToString();
tr.Tag=dataSet1.Tables[0].Rows[i]["CompanyID"].ToString();
treeView1.Nodes.Add(tr);
// MessageBox.Show(tr.Index.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
SeekNode(treeView1.Nodes ,dataSet1.Tables[0].Rows[i]["ParentID"].ToString().Trim(),dataSet1.Tables[0].Rows[i]["DeptName"].ToString(),dataSet1.Tables[0].Rows[i]["CompanyID"].ToString());
}
}
treeView1.Nodes[0].Expand();
}
/// <summary>
/// 递归遍历TreeView
/// </summary>
/// <param name="tru"></param>
/// <param name="s_parentID"></param>
/// <param name="s_text"></param>
/// <param name="s_tag"></param>
private void SeekNode(System.Windows.Forms.TreeNodeCollection tru,string s_parentID ,string s_text,string s_tag)
{ // TreeNodeCollection tndco;
//tndco=treeView1.Nodes;
foreach ( TreeNode tnd in tru )
{
//MessageBox.Show(tnd.Tag.ToString());
try
{ if ( tnd.Tag.ToString() == s_parentID ) {
TreeNode tr=new TreeNode();
tr.Text= s_text;
tr.Tag=s_tag;
tnd.Nodes.Add(tr);
return ;
}
else
{
SeekNode(tnd.Nodes,s_parentID,s_text,s_tag);
}
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
}