C# 将列表值从Windows窗体类返回到另一个类 我相信这非常简单,但遗憾的是,我也是。这是我的课程:
我正试图填满一个树状视图,但那是一整套完全不同的问题。非常感谢您的帮助。多谢各位 试试下面。我用记事本编辑代码,所以请原谅任何语法错误C# 将列表值从Windows窗体类返回到另一个类 我相信这非常简单,但遗憾的是,我也是。这是我的课程:,c#,C#,我正试图填满一个树状视图,但那是一整套完全不同的问题。非常感谢您的帮助。多谢各位 试试下面。我用记事本编辑代码,所以请原谅任何语法错误 public void fillTreeViewByList() { connect.Open(); TreeViewColorDescNodes firstNode = new TreeViewColorDescNodes(); // Everything from here down is probably wrong... t
public void fillTreeViewByList()
{
connect.Open();
TreeViewColorDescNodes firstNode = new TreeViewColorDescNodes(); // Everything from here down is probably wrong...
tvDiscountMaintenance.Nodes.Add("Select All");
//firstNode has the instance of TreeViewColorDescNodes() which has list of items from DB
//So you need to iterate the items from this instance
foreach (string item in firstNode.ColDescNode)
{
//You need to create the tree node with text in item rather than List name
TreeNode colorDesc = new TreeNode(item);
//You need to add the tree node to the tree control rather than the string
tvDiscountMaintenance.Nodes[0].Nodes.Add(colorDesc);
}
}
请描述期望的结果以及哪里出了问题。此外,您的类似乎缺少
class
语句。您正在创建TreeViewColorDescNodes
的新实例,但从未访问它。您是否打算使用例如:firstNode.ColDescNode
而不仅仅是ColDescNode
?我可以在类TreeViewCodesDescNodes中运行存储过程,但是当我用以下命令在表单中实例化该类时:TreeViewColorDescNodes firstNode=new TreeViewColorDescNodes();firstNode.ColDescNode仍然为空。计数=0。我改变了我的表单代码,以符合Mukul Varshney的建议,这似乎是有道理的,但我仍然有同样的问题。请解释“课堂似乎缺少一个‘课堂’陈述”。也许这就是问题所在。谢谢你的帮助。如果我将列表和存储过程移动到表单方法fillTreeViewByList中,那么经过您所做的代码更正,它就可以工作了。但是,如果我尝试将列表从类TreeViewColorDescNodes返回到表单方法fillTreeViewByList,则firstNode仍然为空。计数=0。我几乎放弃了。再次感谢您的帮助。除了在TreeViewColorDescNodes类中,您是否有任何ColDescNode声明?在tvDiscountMaintenance.Nodes.Add(“全选”)处也放置一个断点;看看firstNode.ColDescNode列表的计数是多少?
public void fillTreeViewByList()
{
connect.Open();
TreeViewColorDescNodes firstNode = new TreeViewColorDescNodes(); // Everything from here down is probably wrong...
tvDiscountMaintenance.Nodes.Add("Select All");
foreach (var item in ColDescNode)
{
TreeNode colorDesc = new TreeNode(ColDescNode.ToString());
tvDiscountMaintenance.Nodes[0].Nodes.Add(item.ToString());
}
}
public void fillTreeViewByList()
{
connect.Open();
TreeViewColorDescNodes firstNode = new TreeViewColorDescNodes(); // Everything from here down is probably wrong...
tvDiscountMaintenance.Nodes.Add("Select All");
//firstNode has the instance of TreeViewColorDescNodes() which has list of items from DB
//So you need to iterate the items from this instance
foreach (string item in firstNode.ColDescNode)
{
//You need to create the tree node with text in item rather than List name
TreeNode colorDesc = new TreeNode(item);
//You need to add the tree node to the tree control rather than the string
tvDiscountMaintenance.Nodes[0].Nodes.Add(colorDesc);
}
}