首页 新闻 搜索 专区 学院

我很迷茫的一个问题

-1
悬赏园豆:20 [已解决问题] 解决于 2011-11-08 09:59
private void buildTree(TreeView tv, string kind)
{
tv.Nodes.Clear();
string sql = "";
switch (kind)
{
case "t_RepairItemDetail":
sql
= "select isNull(ItemIDParentID,'') as ItemIDParentID,ItemID,ItemName,ItemStandHours from t_RepairItemDetail where 1=1 order by ItemID";
break;
}


DataTable dt
= DBHelp.ReturnDataTable(sql, kind);
TreeNode tempNode
= null;
TreeNode tempNodeFirst
= null;
string strItemID= "";
string strItemName = "";
string strItemIDParentID = "";


tempNodeFirst
= new TreeNode();
tempNodeFirst.Value
= "";
tempNodeFirst.Text
= "全部";
tv.Nodes.Add(tempNodeFirst);

for (int i = 0; i < dt.Rows.Count; i++)
{

strItemID
= dt.Rows[i]["ItemID"].ToString();
strItemName
= dt.Rows[i]["ItemName"].ToString();
strItemIDParentID
= dt.Rows[i]["ItemIDParentID"].ToString();
if (strItemIDParentID == "")
{
tempNode
= new TreeNode();
tempNode.Value
= strItemID;
tempNode.Text
= strItemID + ":" + strItemName;
tempNodeFirst.ChildNodes.Add(tempNode);
}
else
{
while ( strItemID.Substring(0, ( strItemID.Length <= tempNode.Value.Length ? strItemID.Length : tempNode.Value.Length)) != strItemIDParentID)
{
if (tempNode.Parent != null)
{
tempNode
= ((TreeNode)tempNode.Parent);
}
else
{
tempNode
= new TreeNode();
tempNode.Value
= strItemID;
tempNode.Text
= strItemID + ":" + strItemName;
tempNodeFirst.ChildNodes.Add(tempNode);
break;
}
strItemIDParentID
= tempNode.Value;
}

TreeNode node1
= new TreeNode();
node1.Value
= strItemID;
node1.Text
= strItemID + ":" + strItemName;
tempNode.ChildNodes.Add(node1);
tempNode
= node1;
}
}
tv.ExpandDepth
= 3;
}

有谁能告诉我这段代代码中的这句话中的: while ( strItemID.Substring(0, ( strItemID.Length <= tempNode.Value.Length ?  strItemID.Length : tempNode.Value.Length)) != strItemIDParentID)

tempNode的值是从哪里来的?我这里写的是一个TreeView控件的绑定

HerWorld的主页 HerWorld | 初学一级 | 园豆:111
提问于:2011-06-30 09:00
< >
分享
最佳答案
0

if (strItemIDParentID == "")
{
tempNode
= new TreeNode();
tempNode.Value
= strItemID;
tempNode.Text
= strItemID + ":" + strItemName;
tempNodeFirst.ChildNodes.Add(tempNode);
}

在这里实例化的,如果第一次循环不满足strItemIDParentID == "",那么你的程序应该就要抛异常了

收获园豆:10
镆铘 | 菜鸟二级 |园豆:361 | 2011-06-30 11:07
其他回答(2)
0

上面的if代码块里不是有么

收获园豆:5
today4king | 园豆:3299 (老鸟四级) | 2011-06-30 09:55
0

tempNode = new TreeNode();
tempNode.Value
= strItemID;

你不是在if里面做的处理吗?

收获园豆:5
蓦然回首的思念 | 园豆:900 (小虾三级) | 2011-06-30 11:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册