treeview的问题...
在Vs2005中,我想写Winform小程序
表:
Id bigkind kin_id Smind
1 aa 1 aa11
2 aa 2 aa22
3 bb 1 bb11
4 bb 2 bb11
5 cc 1 cc11
在treeview中显示的格式为:
aa
...aa11
...aa22
bb
...bb11
...bb22
cc
请问该怎么样来实现....(请问我的表这样建立能不能实现上面的树形结构)
------解决方案--------------------DataTable dtBigkind = new DataTable();
DataTable dtSmind = new DataTable();
dtBigkind = publicDataTable.Loader.QuerySql( "select distinct bigkind from mytest ");
for(int i=0;i <dtBigkind.Rows.Count;i++)
{
TreeNode node = treeView1.Nodes.Add(dtBigkind.Rows[i][0].ToString());
dtSmind = publicDataTable.Loader.QuerySql( "select distinct Smind from mytest where bigkind= ' "+dtBigkind.Rows[i][0].ToString()+ " ' ");
for(int j=0;j <dtSmind.Rows.Count;j++)
{
node.Nodes.Add(dtSmind.Rows[j][0].ToString());
}
}
------解决方案--------------------从网上找的一个方法
直接用就可以
private void CreateTree()
{
treeView1.Nodes.Clear();
dv = dtTree.DefaultView;//dtTree 你要做的datatable
dv.Sort = "TypeParentID ASC ";
DataRowView[] arrDRV = dv.FindRows(0);//Get root data info
if (arrDRV.Length == 0) return;
TreeNode tnNew = null;
foreach (DataRowView drv in arrDRV)
{
i = 0;
tnNew = treeView1.Nodes.Add(drv.Row[ "TypeName "].ToString());
tnNew.Tag = drv.Row[ "TypeID "].ToString();//Save "TypeID " in node 's tag
tnNew.ImageIndex = i;
++i;
CreateTreeNode(ref tnNew);
}
}