BindingSource for a class - need a light bulb moment

T

TN

I just don't get the BindingSource class when it is bound to a class.
Consider this code snip:

.....
Private bSource As New BindingSource()
Private dgv As New DataGridView()

Public Sub New()
InitializeComponent()

' Bind the BindingSource to the DemoCustomer type !!!!!!!!!!!!!
bSource.DataSource = GetType(DemoCustomer)

' Set up the DataGridView control.
dgv.Dock = DockStyle.Fill
Me.Controls.Add(dgv)

' Bind the DataGridView control to the BindingSource.
dgv.DataSource = bSource
End Sub
.....

Assuming "DemoCustomer" is a simple class with a few properties, how is
it storing the data. Normally I bind a datasource to an instance of a
datatable or something. Where is the list of DemoCustomer's instances
stored? I am missing something obvious here, yes? Thanks.
 
B

Bart Mermuys

Hi,

TN said:
I just don't get the BindingSource class when it is bound to a class.
Consider this code snip:

....
Private bSource As New BindingSource()
Private dgv As New DataGridView()

Public Sub New()
InitializeComponent()

' Bind the BindingSource to the DemoCustomer type !!!!!!!!!!!!!
bSource.DataSource = GetType(DemoCustomer)

' Set up the DataGridView control.
dgv.Dock = DockStyle.Fill
Me.Controls.Add(dgv)

' Bind the DataGridView control to the BindingSource.
dgv.DataSource = bSource
End Sub
....

Assuming "DemoCustomer" is a simple class with a few properties, how is it
storing the data. Normally I bind a datasource to an instance of a
datatable or something. Where is the list of DemoCustomer's instances
stored? I am missing something obvious here, yes? Thanks.

When you assign a Type of a class (that doesn't implement ITypedList or
IListSource) to the DataSource of a BindingSource, then it will create a
System.ComponentModel.BindingList( Of T ) for the items, eg. :
BindingList( Of DemoCustomer ) .

You can access this BindingList from BindingSource's List property, eg:

Dim list As BindingList( Of DemoCustomer ) = DirectCast( _
bSource.List, BindingList( Of DemoCustomer ) )

AFAIK, you should not assign a Type to the DataSource of a BindingSource
from Code. It's mainly used when you drag a custom object from the Data
Sources Window onto the Form. When you do that there won't be an instance
of the custom object placed on the Form (or custom list). So the DataSource
of the BindingSource will be set to a Type and the BindingSource will create
a BindingList so you can setup everything in the designer. But even then
it's not a bad idea to assign the real DataSource (instance, not Type) at
Form load.


HTH,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top