タイトル : Re: TREEVIEW の使い方 投稿日 : 2009/12/01(Tue) 15:31 投稿者 : 魔界の仮面弁士
#Region "階層で管理される場合" Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click Dim tbl As DataTable = GetSample1() If tbl.Rows.Count = 0 Then Return End If Dim maxLevel As Integer = CInt(tbl.Compute("MAX(層)", "")) Dim nodes(maxLevel + 1) As TreeNodeCollection nodes(0) = TreeView1.Nodes For Each row As DataRow In tbl.Rows Dim lv As Integer = CInt(row("層")) nodes(lv + 1) = nodes(lv).Add(row("ID").ToString(), row("名").ToString()).Nodes Next TreeView1.ExpandAll() End Sub Private Function GetSample1() As DataTable Dim tbl As New DataTable tbl.Columns.Add("ID", GetType(Integer)) tbl.Columns.Add("層", GetType(Integer)) tbl.Columns.Add("名", GetType(String)) tbl.PrimaryKey = New DataColumn() {tbl.Columns("ID")} tbl.Rows.Add(1, 0, "製品A") '製品A tbl.Rows.Add(2, 1, "中間1") '┣中間1 tbl.Rows.Add(3, 2, "部品1") '┃┗部品1 tbl.Rows.Add(4, 1, "中間2") '┗中間2 tbl.Rows.Add(5, 2, "部品2") ' ┣部品2 tbl.Rows.Add(6, 2, "部品3") ' ┗部品3 Return tbl End Function #End Region あらかじめ、データ内容の整合性を検査しておいてください。 第1階層が無いのに、第2階層が存在していた場合などは展開できません。 |