A BindingSource
is a Component
, not a Control
, so it is not in the Controls
collection. However, the designer creates a private IContainer
field called components
to hold all components created on the form, so you can access the components through that field :
For Each c In components.Components
MessageBox.Show(c.ToString())
Next
Unfortunately, components don't have a name, so you will have to find another way to identify your BindingSource
... For instance, if you know that each BindingSource
is bound to a DataTable
, you can check the name of the table.
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
EDIT: the SO syntax highlighter seems to have trouble with VB...