树视图上的循环问题
这可能非常简单,但我遇到了一个非常奇怪的问题,我编写的用于将菜单项填充到树视图的循环不显示子节点 ID。它提取的数据在 sql 上采用以下格式。
OptionID OptionText displayOrder parentOptionID optionDeleted 226 test menu 0 0 FALSE 227 test menu1 0 226 FALSE 228 test menu2 0 227 FALSE 229 test menu 3 0 228 FALSE 230 test 0 229 FALSE 231 test 2 3 228 FALSE 232 test 3 6 229 FALSE
当尝试填充树视图时,它将树中选择节点的parentOptionID输出到标签 - 问题是它没有选择子节点的parentOptionID,在本例中是测试、测试2和测试。这是我正在使用的代码..
private void populateRootLevelAll(bool ireset)
{
// Locals
Functionality func = new Functionality();
SqlConnection supportDB = null;
DataTable t = new DataTable();
bool opDeleted = false;
string spName = "gssp_TechCallTrackerOptionsSelectAllWithDelete";
try
{
using (supportDB = new SqlConnection(getConnectionString(ConnectionType.GriffinSupportDB)))
{
using (SqlCommand getCallTrackerOptions = new SqlCommand(spName, supportDB))
{
// Now set up the rest of the command object
getCallTrackerOptions.CommandType = CommandType.StoredProcedure;
// Populate the parameters.
getCallTrackerOptions.Parameters.Clear();
getCallTrackerOptions.Parameters.Add(func.CreateParameter("@optionDeleted", SqlDbType.Bit, ParameterDirection.Input, opDeleted));
getCallTrackerOptions.Parameters.Add(func.CreateParameter("@spErrorID", SqlDbType.Int, ParameterDirection.Output, DBNull.Value));
// Set up the dataset
supportDB.Open();
SqlDataAdapter da = new SqlDataAdapter(getCallTrackerOptions);
da.Fill(t);
supportDB.Close();
// Build TreeList Nodes.
foreach (DataRow r in t.Rows)
{
// add top level rows and then add their child rows on each itteration.
if (r["parentOptionID"].ToString() == "0")
{
TreeNode masterNode = new TreeNode(r["optionText"].ToString());
if (GetChildNodes(r["OptionID"].ToString(), t) != null)
{
List<TreeNode> childNodeList = GetChildNodes(r["OptionID"].ToString(), t);
masterNode.Nodes.AddRange(childNodeList.ToArray());
}
masterNode.Tag = "0";
masterNode.Name = r["OptionID"].ToString();
TVCTOptions.Nodes.Add(masterNode);
}
}
}
}
}
catch (Exception e)
{
throw e;
}
}
static List<TreeNode> GetChildNodes(string parentOptionID, DataTable t)
{
List<TreeNode> nodeList = new List<TreeNode>();
foreach (DataRow r in t.Rows)
{
if (r["parentOptionID"].ToString() == parentOptionID)
{
// create child
TreeNode node = new TreeNode(r["optionText"].ToString());
node.Tag = parentOptionID.ToString();
// check if this child has children.
if (GetChildNodes(r["OptionID"].ToString(), t) != null)
{
node.Nodes.AddRange(GetChildNodes(r["OptionID"].ToString(), t).ToArray());
node.Name = (r["OptionID"].ToString());
}
else
{
node.Tag = parentOptionID.ToString();
}
nodeList.Add(node);
}
}
if (nodeList.Count != 0)
return nodeList;
else
return null; // returns null when no children were found.
}
谢谢
This maybe something really simple but I got a really strange issue where a loop i have written to populate menu items to a treeview is not displaying childnode id. The data it is pulling is in the following format on the sql.
OptionID OptionText displayOrder parentOptionID optionDeleted 226 test menu 0 0 FALSE 227 test menu1 0 226 FALSE 228 test menu2 0 227 FALSE 229 test menu 3 0 228 FALSE 230 test 0 229 FALSE 231 test 2 3 228 FALSE 232 test 3 6 229 FALSE
When try the treeview is populated it outputs the parentOptionID of the select node in the tree to a label - the problem is it does not pick the the parentOptionID of the childs, in this case test, test 2 and test. This is the code i am using..
private void populateRootLevelAll(bool ireset)
{
// Locals
Functionality func = new Functionality();
SqlConnection supportDB = null;
DataTable t = new DataTable();
bool opDeleted = false;
string spName = "gssp_TechCallTrackerOptionsSelectAllWithDelete";
try
{
using (supportDB = new SqlConnection(getConnectionString(ConnectionType.GriffinSupportDB)))
{
using (SqlCommand getCallTrackerOptions = new SqlCommand(spName, supportDB))
{
// Now set up the rest of the command object
getCallTrackerOptions.CommandType = CommandType.StoredProcedure;
// Populate the parameters.
getCallTrackerOptions.Parameters.Clear();
getCallTrackerOptions.Parameters.Add(func.CreateParameter("@optionDeleted", SqlDbType.Bit, ParameterDirection.Input, opDeleted));
getCallTrackerOptions.Parameters.Add(func.CreateParameter("@spErrorID", SqlDbType.Int, ParameterDirection.Output, DBNull.Value));
// Set up the dataset
supportDB.Open();
SqlDataAdapter da = new SqlDataAdapter(getCallTrackerOptions);
da.Fill(t);
supportDB.Close();
// Build TreeList Nodes.
foreach (DataRow r in t.Rows)
{
// add top level rows and then add their child rows on each itteration.
if (r["parentOptionID"].ToString() == "0")
{
TreeNode masterNode = new TreeNode(r["optionText"].ToString());
if (GetChildNodes(r["OptionID"].ToString(), t) != null)
{
List<TreeNode> childNodeList = GetChildNodes(r["OptionID"].ToString(), t);
masterNode.Nodes.AddRange(childNodeList.ToArray());
}
masterNode.Tag = "0";
masterNode.Name = r["OptionID"].ToString();
TVCTOptions.Nodes.Add(masterNode);
}
}
}
}
}
catch (Exception e)
{
throw e;
}
}
static List<TreeNode> GetChildNodes(string parentOptionID, DataTable t)
{
List<TreeNode> nodeList = new List<TreeNode>();
foreach (DataRow r in t.Rows)
{
if (r["parentOptionID"].ToString() == parentOptionID)
{
// create child
TreeNode node = new TreeNode(r["optionText"].ToString());
node.Tag = parentOptionID.ToString();
// check if this child has children.
if (GetChildNodes(r["OptionID"].ToString(), t) != null)
{
node.Nodes.AddRange(GetChildNodes(r["OptionID"].ToString(), t).ToArray());
node.Name = (r["OptionID"].ToString());
}
else
{
node.Tag = parentOptionID.ToString();
}
nodeList.Add(node);
}
}
if (nodeList.Count != 0)
return nodeList;
else
return null; // returns null when no children were found.
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。我需要返回nodeList;在其他之后。
Worked it out. I needed return nodeList; after the else.