471,122 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

BindingSource for a class - need a light bulb moment

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.
Jan 13 '06 #1
1 4403
Hi,

"TN" <ti*******@phreaker.net> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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
Jan 13 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Trammel | last post: by
1 post views Thread by Leonardo | last post: by
2 posts views Thread by jehugaleahsa | last post: by
1 post views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
5 posts views Thread by jehugaleahsa | last post: by
2 posts views Thread by Eric B. | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.