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
Worked it out. I needed return nodeList; after the else.
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 nodeList;
}
精彩评论