Compartir por


BindingSource.DataSource Propiedad

Definición

Obtiene o establece el origen de datos al que se enlaza 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

que Object actúa como origen de datos. El valor predeterminado es null.

Ejemplos

En el ejemplo de código siguiente se asigna una lista de clientes al DataSource de un BindingSource componente. Este ejemplo de código forma parte de un ejemplo más grande proporcionado 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 comunes y la evaluación de lista resultante.

DataSource (propiedad) Enumerar los resultados
null IBindingList Vacío de objetos. Al agregar un elemento, se establece la lista en el tipo del elemento agregado.
null con DataMember set No se admite, genera ArgumentException.
Tipo de no lista o objeto de tipo "T" Vacío IBindingList de tipo "T".
Instancia de matriz IBindingList que contiene los elementos de matriz.
IEnumerable instancia de que IBindingList contiene los IEnumerable elementos.
Instancia de lista que contiene el tipo "T" IBindingList instancia que contiene el tipo "T".

Además, DataSource se puede establecer en otros tipos de lista, como IListSource y ITypedList y los BindingSource controlará adecuadamente. 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 propiedad DataSource , 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 para la BindingSource clase .

Se aplica a

Consulte también