C# 递归的应用 TreeView递归绑定数据 代码运行错误,请帮忙修改代码
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data .SqlClient ;
using System.Windows.Forms;
namespace @new
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlDataAdapter adp= new SqlDataAdapter("select * from Table1", connString);
DataTable dtData=new DataTable();
adp.Fill(dtData);
TreeView tv;//树控件
// tv.Nodes.Clear();
// 获得第一级数据
DataRow[] drRoot=dtData.Select("len(varid)=3");
foreach(DataRow dr in drRoot)
{
TreeNode tn=new TreeNode();
tn.Text=dr["Name"].ToString();
AppendChild(tn,dr["varid"].ToString());
tv.Nodes.Add(tn);
}
}
// 递归绑定数据
Private void AppendChild(TreeNode tnParent,string varid)
{
DataRow[] drs=dtData.Select("substring(varid,0,"+varid.length+" and len(varid)="+varid.length+3));
foreach(DataRow dr in drs)
{
TreeNode tn=new TreeNode();
tn.Text=dr["Name"].ToString();
AppendChild(tn,dr["varid"].ToString());
tnParent.ChildNodes.Add(tn);
}
}
} 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data .SqlClient ;
using System.Windows.Forms;
namespace @new
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlDataAdapter adp= new SqlDataAdapter("select * from Table1", connString);
DataTable dtData=new DataTable();
adp.Fill(dtData);
TreeView tv;//树控件
// tv.Nodes.Clear();
// 获得第一级数据
DataRow[] drRoot=dtData.Select("len(varid)=3");
foreach(DataRow dr in drRoot)
{
TreeNode tn=new TreeNode();
tn.Text=dr["Name"].ToString();
AppendChild(tn,dr["varid"].ToString());
tv.Nodes.Add(tn);
}
}
// 递归绑定数据
Private void AppendChild(TreeNode tnParent,string varid)
{
DataRow[] drs=dtData.Select("substring(varid,0,"+varid.length+" and len(varid)="+varid.length+3));
foreach(DataRow dr in drs)
{
TreeNode tn=new TreeNode();
tn.Text=dr["Name"].ToString();
AppendChild(tn,dr["varid"].ToString());
tnParent.ChildNodes.Add(tn);
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询