从 Sql Server 2008 在 TreeView 中添加父节点和子节点
我想将父节点和子节点从 sql server 添加到树视图。我实现了一些代码。但我收到错误“索引超出范围”,
下面是我用来填充父节点和子节点的代码。
protected void GetParentNodes()
{
SqlDataAdapter adap = new SqlDataAdapter("select id, name from crossarticle_category where parentid=-1", con);
DataTable dt = new DataTable();
adap.Fill(dt);
int index = -1;
foreach (DataRow d in dt.Rows)
{
SqlDataAdapter adapInner = new SqlDataAdapter("select id, name from crossarticle_category where parentid=" + Convert.ToInt32(d["id"].ToString()) + "", con);
DataTable dtInner = new DataTable();
adapInner.Fill(dtInner);
index++;
TreeNode n = new TreeNode();
n.Value = d["id"].ToString();
n.Text = d["name"].ToString();
foreach (DataRow r in dtInner.Rows)
{
if (dtInner.Rows.Count > 0)
{
TreeNode inner = new TreeNode();
inner.Value = r["id"].ToString();
inner.Text = r["name"].ToString();
tree1.Nodes[index].ChildNodes.Add(inner);
}
}
tree1.Nodes.Add(n);
}
}
任何人都可以帮我纠正此代码中的问题..此代码是我自己创建的。
i want to add the parent and child nodes from sql server to treeview. i implemented some code. but i gets error "Index was out of range"
below is the code i am using to fill parent and child nodes.
protected void GetParentNodes()
{
SqlDataAdapter adap = new SqlDataAdapter("select id, name from crossarticle_category where parentid=-1", con);
DataTable dt = new DataTable();
adap.Fill(dt);
int index = -1;
foreach (DataRow d in dt.Rows)
{
SqlDataAdapter adapInner = new SqlDataAdapter("select id, name from crossarticle_category where parentid=" + Convert.ToInt32(d["id"].ToString()) + "", con);
DataTable dtInner = new DataTable();
adapInner.Fill(dtInner);
index++;
TreeNode n = new TreeNode();
n.Value = d["id"].ToString();
n.Text = d["name"].ToString();
foreach (DataRow r in dtInner.Rows)
{
if (dtInner.Rows.Count > 0)
{
TreeNode inner = new TreeNode();
inner.Value = r["id"].ToString();
inner.Text = r["name"].ToString();
tree1.Nodes[index].ChildNodes.Add(inner);
}
}
tree1.Nodes.Add(n);
}
}
can anyone help me rectify the issue in this code.. this code has been created by myself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试在添加父节点之前添加子节点。尝试先添加父节点,然后添加子节点;像这样:
然后
Looks like you are trying to add a child node before you add the parent. Try adding the parent first and then the child nodes; like so:
And then