C#TreeView错误超出范围索引 [英] C# TreeView Error Out of Range Index

查看:81
本文介绍了C#TreeView错误超出范围索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我从数据库中提取的数据动态构建树状视图.一些父节点有一个子节点,而另一些则没有,而另一些则没有子节点.我试图使用下面的代码,但出现编译器错误指定的参数超出有效值范围."我尝试了几种不同的方法来做到这一点,但是没有运气.感谢您的帮助.

这是数据的一个例子

I''m attempting to dynamically build a treeview from data that I''m pulling out of a database. Some parent nodes have a child node while others do not, and some have a grand child while other do not. I''m attempting to use the code below but I''m getting a compiler error "Specified argument was out of range of valid values." I''ve tried few different ways of doing this but no luck. Thanks for you help.

Here''s an example of the data

CategoryName   SubCategoryName   SubCategory2Name 
ASV            G-1                 Error 
ASV            SV                  Error 
LYT     
INS            Script   
INS            Export             cc07 
INS            How To   
INS            Notes   




如果我使用以下内容:




If I use the following:

tn.Nodes.Add(dr["Child"].ToString());



我得到的输出看起来像
ASV(根)
g-1
SV
剧本
出口
如何
笔记




I get an output that looks something like
ASV (root)
g-1
SV
Script
Export
How To
Notes


treeView1.BeginUpdate();
                
foreach(DataRow r in parent.Rows)
{ 	
   tn = treeView1.Nodes.Add(r["ID"].ToString(), r["Parent"].ToString());
                	
   foreach(DataRow dr in child.Rows)
   {
     treeView1.Nodes[treeView1.Nodes.IndexOf(tn.Parent)].Nodes.Add
     (dr["Child"].ToString());
   }
                	
}
treeView1.EndUpdate();

推荐答案

想到的第一个问题是:您是否在CP和StackOverFlow以及MS文档中搜索了如何进行数据处理绑定到TreeView吗?

第二:您是否正在处理以下情况:TreeView中可能的嵌套深度"为#2或#3?

我的印象是,您描述了TreeView本身具有#n个根级节点的情况,每个根节点都对应于DataBase的parent.Rows中的Row.根节点的父对象"始终为"null".

因此,当您提到孙子"时,我不确定您的节点深度是#2还是#3.

您的代码仅显示子节点的内部解析的一个级别,最大深度为#2,除非您认为根节点"在某种意义上确实具有虚拟深度"#1,其父代为"null".

我无法在此处评估的另一种可能性是,您是否可以任意深度嵌套:您显然说某些节点可以有子节点,而其他节点可能没有子节点,但是可以存在除#2以外的具有许多嵌套级别的节点,或#3. ?

在没有成功的数据绑定策略的情况下(恕我直言,这是首选的解决方案),如果您确实打算将Nodes嵌套在任意深的级别,则将需要编写递归代码来填充TreeView.

在您的示例代码中,我有几个奇怪的方面:

1.``child.rows是从哪里来的:它们是否不应该通过外部循环每次都从parent.rows派生出来?

2.您要添加一个根级节点:''tn

一个.但是,然后,您没有将子节点添加到"tn",而是将它们添加到"tn"的计算出的父节点:如果"tn是根节点:其父节点为" null,并且有错误"在那里.

关于您的数据库结构以及您的确切意图,您会更加清楚一点,希望我能对您有所帮助. "parent.rows"和"child.rows"到底指的是什么?

同时,考虑一下(作为疯狂的猜测"):
The first question that comes to mind is: have you searched CP and StackOverFlow and the MS docs for how to data bind to a TreeView ?

Second: Are you dealing with a case here where: the possible "depth of nesting" in the TreeView is #2 or #3 ?

My impression is that you describe a case where the TreeView itself has #n Root-level Nodes, each of which corresponds to a Row in the parent.Rows of your DataBase. The "Parent" of a Root Node is always "null."

So, when you mention "grandchild" I''m not sure if you have a potential node depth of #2 or #3.

Your code shows only one level of inner parsing of child nodes, giving you a maximum depth of #2, unless you are thinking that a "root node" does, in a sense, have a "virtual depth" of #1, in spite of its parent being "null."

The other possibility I can''t evaluate here is whether you have arbitrary depths of nesting possible: clearly you say some nodes can have children, but others may not, but can there be nodes that have many levels of nesting, beyond #2, or #3. ?

In the absence of a successful data binding strategy: which, imho, is the solution of choice, you are going to need to write recursive code to populate the TreeView, if you do, indeed, intend to have Nodes nested at arbitrarily deep levels.

In your sample code there are several strange aspects to me:

1. where do ''child.rows come from: shouldn''t they be derived from parent.rows each time through the outer loop ?

2. you are adding a root-level node: ''tn

a. but then, instead of adding the child nodes to ''tn, you adding them to the calculated parent of ''tn: if ''tn is a root node: its parent is ''null, and there''s an error right there.

A little more clarity from you on what your DB structure is, and your exact intent, and I hope I can be more helpful. What exactly do ''parent.rows'' and ''child.rows'' refer to ?

Meanwhile, consider (as a "wild guess"):
treeView1.BeginUpdate();

foreach(DataRow r in parent.Rows)
{
   tn = treeView1.Nodes.Add(r["ID"].ToString(), r["Parent"].ToString());

   foreach(DataRow dr in ?) // replace '?' with something that retruns child DataRows of 'r !
   {
      tn.Nodes.Add(dr["Child"].ToString());
   }

}
treeView1.EndUpdate();


这篇关于C#TreeView错误超出范围索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆