Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
BindingSource Class
 DataSource Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
BindingSource..::.DataSource Property

Gets or sets the data source that the connector binds to.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Property DataSource As Object
Visual Basic (Usage)
Dim instance As BindingSource
Dim value As Object

value = instance.DataSource

instance.DataSource = value
C#
public Object DataSource { get; set; }
Visual C++
public:
property Object^ DataSource {
    Object^ get ();
    void set (Object^ value);
}
JScript
public function get DataSource () : Object
public function set DataSource (value : Object)

Property Value

Type: System..::.Object
An Object that acts as a data source. The default is nullNothingnullptra null reference (Nothing in Visual Basic).

The DataSource property can be set to a number of data sources, including types, objects, and lists of types. The resulting data source will be exposed as a list. The following table shows some of the common data sources and the resulting list evaluation.

DataSource property

List results

nullNothingnullptra null reference (Nothing in Visual Basic)

An empty IBindingList of objects. Adding an item sets the list to the type of the added item.

nullNothingnullptra null reference (Nothing in Visual Basic) with DataMember set

Not supported, raises ArgumentException.

Non-list type or object of type "T"

Empty IBindingList of type "T".

Array instance

IBindingList containing the array elements.

IEnumerable instance

An IBindingList containing the IEnumerable items.

List instance containing type "T"

IBindingList instance containing type "T".

In addition, DataSource can be set to other list types such as IListSource and ITypedList and the BindingSource will handle them appropriately. In this case, the type contained in the list should have a default constructor.

When setting a data source, if the supplied reference contains more than one list or table, you must set the DataMember property to a string that specifies the list to bind to. Setting this property raises the DataSourceChanged event.

The DataSource property is the default property for the BindingSource class.

The following code example assigns a list of customers to the DataSource of a BindingSource component. This code example is part of a larger example provided at How to: Raise Change Notifications Using the BindingSource ResetItem Method.

Visual Basic
Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Me.Load
    ' Create and populate the list of DemoCustomer objects
    ' which will supply data to the DataGridView.
    Dim customerList As List(Of DemoCustomer) = _
    New List(Of DemoCustomer)
    customerList.Add(DemoCustomer.CreateNewCustomer())
    customerList.Add(DemoCustomer.CreateNewCustomer())
    customerList.Add(DemoCustomer.CreateNewCustomer())

    ' Bind the list to the BindingSource.
    Me.customersBindingSource.DataSource = customerList

    ' Attach the BindingSource to the DataGridView.
    Me.customersDataGridView.DataSource = Me.customersBindingSource
End Sub

C#
private void Form1_Load(System.Object sender, System.EventArgs e)
{
    // Create and populate the list of DemoCustomer objects
    // which will supply data to the DataGridView.
    List<DemoCustomer> customerList = new List<DemoCustomer>();
    customerList.Add(DemoCustomer.CreateNewCustomer());
    customerList.Add(DemoCustomer.CreateNewCustomer());
    customerList.Add(DemoCustomer.CreateNewCustomer());

    // Bind the list to the BindingSource.
    this.customersBindingSource.DataSource = customerList;


    // Attach the BindingSource to the DataGridView.
    this.customersDataGridView.DataSource = 
        this.customersBindingSource;
}

Visual C++
void Form1_Load( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   // Create and populate the list of DemoCustomer objects
   // which will supply data to the DataGridView.
   List< DemoCustomer^ >^ customerList = gcnew List< DemoCustomer^ >;
   customerList->Add( DemoCustomer::CreateNewCustomer() );
   customerList->Add( DemoCustomer::CreateNewCustomer() );
   customerList->Add( DemoCustomer::CreateNewCustomer() );

   // Bind the list to the BindingSource.
   this->customersBindingSource->DataSource = customerList;

   // Attach the BindingSource to the DataGridView.
   this->customersDataGridView->DataSource =
      this->customersBindingSource;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker