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控件的绑定
if (strItemIDParentID == "")
{
tempNode = new TreeNode();
tempNode.Value = strItemID;
tempNode.Text = strItemID + ":" + strItemName;
tempNodeFirst.ChildNodes.Add(tempNode);
}
在这里实例化的,如果第一次循环不满足strItemIDParentID == "",那么你的程序应该就要抛异常了
上面的if代码块里不是有么
tempNode = new TreeNode();
tempNode.Value = strItemID;
你不是在if里面做的处理吗?