-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBindingController.vb
83 lines (66 loc) · 3.21 KB
/
DataBindingController.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
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
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Windows.Forms
Namespace RichEditCustomInsertMergeFieldMenu
Public Class DataBindingController
Private Shared bindingContext As BindingContext = New BindingContext()
Private dataSource As Object
Private dataMember As String
Private bindingManager As BindingManagerBase = Nothing
Private itemProperties As PropertyDescriptorCollection = Nothing
Public Sub New(ByVal dataSource As Object, ByVal dataMember As String)
Me.dataSource = dataSource
Me.dataMember = dataMember
InitializeBindingManagerAndItemProperties()
End Sub
Private Sub InitializeBindingManagerAndItemProperties()
If dataSource IsNot Nothing Then
If dataMember.Length > 0 Then
bindingManager = bindingContext(dataSource, dataMember)
Else
bindingManager = bindingContext(dataSource)
End If
End If
If bindingManager IsNot Nothing Then itemProperties = bindingManager.GetItemProperties()
End Sub
Public ReadOnly Property ItemsCount As Integer
Get
Return If(bindingManager IsNot Nothing, bindingManager.Count, 0)
End Get
End Property
Public ReadOnly Property ColumnNames As List(Of String)
Get
Dim list As List(Of String) = New List(Of String)()
If itemProperties IsNot Nothing Then
Dim count As Integer = itemProperties.Count
For i As Integer = 0 To count - 1
list.Add(itemProperties(i).Name)
Next
End If
Return list
End Get
End Property
Public Function GetColumnInfo(ByVal columnName As String) As PropertyDescriptor
If itemProperties IsNot Nothing Then
Dim prop As PropertyDescriptor = itemProperties.Find(columnName, False)
If prop Is Nothing Then Throw New ArgumentException(String.Format("'{0}' column does not exist", columnName))
Return If(itemProperties IsNot Nothing, itemProperties(columnName), Nothing)
End If
Return Nothing
End Function
Public Function GetRowValue(ByVal columnName As String, ByVal rowIndex As Integer) As Object
If bindingManager IsNot Nothing AndAlso itemProperties IsNot Nothing Then
Dim prop As PropertyDescriptor = itemProperties.Find(columnName, False)
If prop Is Nothing Then Throw New ArgumentException(String.Format("'{0}' column does not exist", columnName))
If TypeOf bindingManager Is CurrencyManager Then
Return prop.GetValue(CType(bindingManager, CurrencyManager).List(rowIndex))
Else
If rowIndex <> 0 Then Throw New ArgumentOutOfRangeException("rowIndex")
Return prop.GetValue(CType(bindingManager, PropertyManager).Current)
End If
End If
Return Nothing
End Function
End Class
End Namespace