BindingSource.DataSource Propiedad

Definición

Obtiene o establece el origen de datos con el que está enlazado el conector.

public:
 property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public object DataSource { get; set; }
public object? DataSource { get; set; }
member this.DataSource : obj with get, set
Public Property DataSource As Object

Valor de propiedad

Objeto Object que funciona como origen de datos. De manera predeterminada, es null.

Ejemplos

En el ejemplo de código siguiente se asigna una lista de clientes a DataSource de un BindingSource componente. Este ejemplo de código forma parte de un ejemplo más grande que se proporciona en How to: Raise Change Notifications Using the BindingSource ResetItem Method.

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;
}
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;
}
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

Comentarios

La DataSource propiedad se puede establecer en una serie de orígenes de datos, incluidos tipos, objetos y listas de tipos. El origen de datos resultante se expondrá como una lista. En la tabla siguiente se muestran algunos de los orígenes de datos habituales y la evaluación de lista resultante.

Propiedad DataSource Resultados de la lista
null IBindingList de objetos vacío. Al agregar un elemento, se establece la lista en el tipo del elemento agregado.
null con DataMember set No se admite, genera ArgumentException.
Tipo diferente de una lista u objeto del tipo "T" Vacío IBindingList de tipo "T".
Instancia de matriz IBindingList que contiene los elementos de la matriz.
Instancia de IEnumerable que IBindingList contiene los IEnumerable elementos.
Instancia de lista que contiene el tipo "T" IBindingList instancia de que contiene el tipo "T".

Además, DataSource se puede establecer en otros tipos de lista, como IListSource y ITypedList , y los BindingSource controlará correctamente. En este caso, el tipo contenido en la lista debe tener un constructor sin parámetros.

Al establecer un origen de datos, si la referencia proporcionada contiene más de una lista o tabla, debe establecer la DataMember propiedad en una cadena que especifique la lista a la que se va a enlazar. Al establecer esta propiedad, se genera el DataSourceChanged evento .

Nota

Si realiza algún cambio en el valor de la DataSource propiedad, debe hacerlo en el subproceso de la interfaz de usuario (UI) para asegurarse de que la interfaz de usuario refleje los cambios.

La DataSource propiedad es la propiedad predeterminada de la BindingSource clase .

Se aplica a

Consulte también