A BindingSource
является Component
, а не Control
, поэтому его нет в коллекции Controls
. Однако дизайнер создает личное поле IContainer
с именем components
для хранения всех компонентов, созданных в форме, поэтому вы можете получить доступ к компонентам через это поле:
For Each c In components.Components
MessageBox.Show(c.ToString())
Next
К сожалению, компоненты не имеют имени, поэтому вам придется найти другой способ идентифицировать ваш BindingSource
... Например, если вы знаете, что каждый BindingSource
связан с DataTable
, можете проверить название таблицы.
Private Function GetBindingSource(ByVal tableName As String) As BindingSource
For Each c In components.Components
Dim bs As BindingSource = TryCast(c, BindingSource)
' If the component is a BindingSource
If bs IsNot Nothing Then
Dim dt As DataTable = TryCast(bs.DataSource, DataTable)
' If the DataSource is a DataTable
If dt IsNot Nothing Then
' Check the table name against the parameter
If dt.TableName = tableName Then
' Found it !
Return bs
End If
End If
End If
Next
' Oops, BindingSource not found
Return Nothing
End Function
РЕДАКТИРОВАТЬ: кажется, что подсветка синтаксиса SO имеет проблемы с VB ...