我有以下代码在Treeview中填充数据。如何使其递归,以便我将有多个图层? 目前显示如下,但我想将其显示为三层。
Here is how it looks like right now
但我正在寻找bomitem= '500101' && bomrev='B'
500101
----100200
----300255
----100214
treeView2.Nodes.Clear();
SqlCommand cmd = new SqlCommand("select * from BOMDETAIL ORDER BY partId", cn);
try
{
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
TreeNode node = new TreeNode(dr["bomItem"].ToString());
node.Nodes.Add(dr["partId"].ToString());
//node.Nodes.Add(dr["qty"].ToString());
node.Nodes.Add(dr["bomRev"].ToString());
treeView2.Nodes.Add(node);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
答案 0 :(得分:0)
你在找这样的东西吗?
private void button_Click(object sender, EventArgs e)
{
treeView2.Nodes.Clear();
SqlCommand cmd = new SqlCommand("select * from BOMDETAIL ORDER BY partId", cn);
try
{
SqlDataReader dr = cmd.ExecuteReader();
SomeFun(dr, treeView2);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SomeFun(SqlDataReader dr, TreeView treeView2)
{
if(dr.Read())
{
TreeNode node = new TreeNode(dr["bomItem"].ToString());
node.Nodes.Add(dr["partId"].ToString();
node.Nodes.Add(dr["bomRev"].ToString());
treeView2.Nodes.Add(node);
SomeFun(dr, treeView2);
}
}
答案 1 :(得分:0)
我的建议 1.创建名为BOMDETAIL的新类,并创建该类的属性,表中有多少列,每个列都有一个属性,如bomItem,partId等... 2.为该类前列表创建集合。并为BOMDETAIL类创建对象,并为while循环中的dr变量赋值给每个属性,每个BOMDETAIL类obect添加List。
ex:List lstBom = new List 而(dr.Read())
BOMDETAIL bom= new BOMDETAIL();
bom.bomItem = dr["bomItem"].ToString();
....
lstBom.add(b);
感谢您的提问