This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
/
SaveCheckHelper.vb
52 lines (47 loc) · 1.69 KB
/
SaveCheckHelper.vb
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
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports DevExpress.XtraTreeList
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraTreeList.Columns
Namespace SaveCheckStateTreeListNode
Friend Class SaveCheckHelper
Private ReadOnly _treeList As TreeList
Private ReadOnly _source As DataTable
Private ReadOnly _indexCheckColumn As Integer
Public Sub New(ByVal treeList As TreeList, ByVal field As String)
_treeList = treeList
Dim column As TreeListColumn = _treeList.Columns.ColumnByFieldName(field)
If column Is Nothing Then
Throw New Exception(String.Format("TreeList doesn't contain the {0} field", field))
End If
_source = TryCast(_treeList.DataSource, DataTable)
_indexCheckColumn = column.ColumnHandle
column.Visible = False
SubscribeEvents()
End Sub
Private Sub SubscribeEvents()
AddHandler _treeList.AfterCheckNode, AddressOf _treeList_AfterCheckNode
AddHandler _treeList.NodesReloaded, AddressOf _treeList_NodesReloaded
End Sub
Private Sub _treeList_NodesReloaded(ByVal sender As Object, ByVal e As EventArgs)
If _treeList.DataSource Is Nothing Then
Return
End If
For i As Integer = 0 To _source.Rows.Count - 1
_treeList.FindNodeByID(i).CheckState = CType(_source.Rows(i)(_indexCheckColumn), CheckState)
Next i
End Sub
Private Sub _treeList_AfterCheckNode(ByVal sender As Object, ByVal e As NodeEventArgs)
If _treeList.DataSource Is Nothing Then
Return
End If
_treeList.BeginUpdate()
_source.Rows(e.Node.Id)(_indexCheckColumn) = _treeList.FindNodeByID(e.Node.Id).CheckState
_treeList.EndUpdate()
End Sub
End Class
End Namespace