1

I am working on a maintenance windows forms application with Tabbed Interface. In the form there are 3 binding sources (lets call it BindingSource1, BindingSource2, BindingSource3). I am trying to optimize the code and want to dynamically access the BindingSource something like this:

objBindingSource = Ctype(Me.Controls("BindingSource" + SelectedBindingSourceID),BindingSource)

I know that it cannot be accomplished using CType since a Control cannot be cast into BindingSource.

Any ideas on how to accomplish this would be great.

Thanks,

Raja

2 Answers 2

3

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...

3
  • Even though it is a long route it is definitely worth exploring....Thank you so much :-)
    – Raja
    Feb 24, 2010 at 12:45
  • The syntax highlighter did not recognize VB, because the question has no VB tag. I added HTML comments: <!-- language: lang-vb --> to fix it. See: Markdown help: Syntax highlighting for code. May 3, 2017 at 12:51
  • Before an actual data source is assigned, the DataSource property has a Type object assigned by the WinForms designer, which designates the type of the bound objects (not the type of their collection), when you are using object binding. So, you can use this to identify the right binding source. May 3, 2017 at 13:01
0

Personally, if there are only three BindingSources, why not just access them directly instead of via a collection? If it's just to pretty up the code to be able to run it through a loop, I don't see much benefit. However, if that's what you want to do, one way to do this would be to initialize the BindingSources within the Form or UserControl's constructor (outside the InitializeComponents method) and add them to the Components collection manually. Doing it this way will allow you to assign a name as a key to the BindingSource within the Components collection. You can then access them like so: (forgive my C#, I'm not that fluent in VB, but you'll get the jist)

BindingSource bs1 = new BindingSource();
BindingSource bs2 = new BindingSource();
BindingSource bs3 = new BindingSource();

// set properties on BindingSources....

// add BindingSources to componenents collection manually.
// add a name key
components.Add(bs1, "BindingSource1");
components.Add(bs2, "BindingSource2");
components.Add(bs3, "BindingSource3");

// access the BindingSource
BindingSource bsSelected = components.Components["BindingSource" + SelectedBindingSourceID] as BindingSource;
if (bsSelected == null)
{
    throw new Exception("BindingSource" + 
                         SelectedBindingSourceID + " doesn't exist");
}

It's not pretty, but it may help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.