本帖最后由 huangsandy 于 2018-6-29 00:33 编辑
只要加这一句程序就报错,不加的话,二层树结构读取正常,那位大神帮我看看什么情况;先谢谢了!
//NodesBind(tn1); //递归操作
//一级树节点读取方法:
private void TreeOne()
{
string sql = string.Format("select * from [Tree_1] where father=-1");
sda = new SqlDataAdapter(sql, DBhelper.Connection); //利用SqlDataAdapter提取数据库,其中Sql为查询语法;DBhelper.Connection为定义好的连接对象
sda.Fill(ds, "TreeOne1"); //填充DataSet数据集
DataTable dt = ds.Tables["TreeOne1"]; //给数据表[dt]赋值;
foreach (DataRow dr in dt.Rows)
{
string xh = dr["xh"].ToString().Trim(); //取得索引号,用于后面的查询
string mc = dr["mc"].ToString().Trim(); //取得部名名称
TreeNode tn1= new TreeNode(mc);
treeView1.Tag = xh;
NodesBind(tn1); //调用方法NodesBind
treeView1.Nodes.Add(tn1); //增加节点
}
}
//添加子节点
private void NodesBind(TreeNode tn1)
{
string sql1 = string.Format("select * from [Tree_1] where father={0}", treeView1.Tag);
sda = new SqlDataAdapter(sql1, DBhelper.Connection); //利用SqlDataAdapter提取数据库,其中Sql为查询语法;DBhelper.Connection为定义好的连接对象
sda.Fill(ds, "TreeTwo1"); //填充DataSet数据集
DataTable dt1 = ds.Tables["TreeTwo1"]; //给数据表[dt]赋值;
foreach (DataRow dr1 in dt1.Rows)
{
string xh_1 = dr1["xh"].ToString().Trim(); //取得索引号,用于后面的查询
string mc_1 = dr1["mc"].ToString().Trim(); //取得部名名称
TreeNode tnn = new TreeNode(mc_1);
treeView1.Tag = xh_1;
//NodesBind(tn1); //递归操作
tn1.Nodes.Add(tnn); //增加节点
}
ds.Tables.Remove("TreeTwo1");
return;
}
|