VB.NET 트리뷰 유동적으로
2019. 4. 2. 11:55ㆍ카테고리 없음
728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 |
Public Class Form1
Public mktrv As TreeViewMaker = New TreeViewMaker()
Public Sub New()
' 디자이너에서 이 호출이 필요합니다.
InitializeComponent()
' InitializeComponent() 호출 뒤에 초기화 코드를 추가하세요.
Dim strCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=testdb.accdb;"
mktrv._dtable = mktrv.Access_to_datatable(strCon, "SELECT * FROM test_info")
mktrv.TView = TreeView1
mktrv.BuildTree(New Integer() {1, 2, 3, 4, 5, 6, 7})
End Sub
End Class
Public Class TreeViewMaker
Private _columns() As Integer
Private _lastIndexOfColumns As Integer Private _tview As TreeView Private dttable As DataTable
Public Property _dtable As DataTable
Get
Return dttable
End Get
Set(value As DataTable)
dttable = value
End Set
End Property
Public Property TView As TreeView
Get
Return _tview
End Get
Set(value As TreeView)
_tview = value
End Set
End Property
#Region "<Summary> accdb to Datatable </Summary>"
Public Function Access_to_datatable(ByRef connstring As String, ByRef sql As String) As DataTable
Dim dt As New System.Data.DataTable
Using cn As New System.Data.OleDb.OleDbConnection With {.ConnectionString = connstring}
Using cmd As New System.Data.OleDb.OleDbCommand With {.Connection = cn}
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = sql
cn.Open()
Using da As New System.Data.OleDb.OleDbDataAdapter(cmd)
Try
da.Fill(dt)
Catch ex As Exception
End Try
End Using
cn.Close()
End Using
End Using
Return dt
End Function
#End Region
Public Sub BuildTree(columns() As Integer, Optional expandAll As Boolean = False)
_columns = columns
_lastIndexOfColumns = columns.Length - 1
_tview.BeginUpdate()
_tview.Nodes.Clear()
'검색하면서 노드 추가
For Each row As DataRow In _dtable.Rows
Dim node As TreeNode
Dim columnIndex As Integer = 0
node = SearchNode(Nothing, row, columnIndex)
If (node Is Nothing) Or (columnIndex < _columns.Length) Then
AddNode(node, row, columnIndex)
End If
Next
If expandAll Then
TView.ExpandAll()
End If
_tview.EndUpdate()
End Sub
Private Function SearchNode(parent As TreeNode, row As DataRow, ByRef columnIndex As Integer) As TreeNode
Dim nodes As TreeNodeCollection
Dim node As TreeNode = Nothing
nodes = If(parent Is Nothing, _tview.Nodes, parent.Nodes)
For Each item As TreeNode In nodes
If String.CompareOrdinal(item.Text, row(_columns(columnIndex)).ToString()) = 0 Then
columnIndex = columnIndex + 1
If columnIndex >= _columns.Length Then
Return item
Else
Return SearchNode(item, row, columnIndex)
End If
Else
node = item.Parent
End If
Next
Return If(columnIndex > 0, node, Nothing)
End Function
Private Sub AddNode(ByRef parent As TreeNode, row As DataRow, ByRef columnIndex As Integer)
If parent Is Nothing Then
_tview.Nodes.Add(row(_columns(0)).ToString())
parent = _tview.Nodes(_tview.GetNodeCount(False) - 1)
columnIndex = columnIndex + 1
End If
For i As Integer = columnIndex To _lastIndexOfColumns
parent.Nodes.Add(row(_columns(i)).ToString())
parent = parent.Nodes(parent.GetNodeCount(False) - 1)
Next
End Sub
End Class |
cs |
728x90
반응형