这可能是非常简单但我有一个非常奇怪的问题,我编写的用于将菜单项填充到树视图的循环不显示childnode 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
当尝试填充树视图时,它会将树中select节点的parentOptionID输出到标签 - 问题是它不会选择子节点的parentOptionID,在本例中为test,test 2和test。这是我正在使用的代码..
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.
}
由于
答案 0 :(得分:0)
解决了这个问题。我需要返回nodeList;在其他人之后。
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;
}