PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET BindingSource for a class - need a light bulb moment

Reply

BindingSource for a class - need a light bulb moment

 
Thread Tools Rate Thread
Old 13-01-2006, 03:30 PM   #1
TN
Guest
 
Posts: n/a
Default BindingSource for a class - need a light bulb moment


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.
  Reply With Quote
Old 13-01-2006, 06:53 PM   #2
Bart Mermuys
Guest
 
Posts: n/a
Default Re: BindingSource for a class - need a light bulb moment

Hi,

"TN" <timnelson@phreaker.net> wrote in message
news:O9DH4YFGGHA.3856@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


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off