|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
BindingSource for a class - need a light bulb momentConsider 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. Hi,
Show quoteHide quote "TN" <timnel***@phreaker.net> wrote in message When you assign a Type of a class (that doesn't implement ITypedList or 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. 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 |
|||||||||||||||||||||||