BindingSource.DataSource Proprietà

Definizione

Ottiene o imposta l'origine dati a cui il connettore è associato.

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

Valore della proprietà

Object che funge da origine dati. Il valore predefinito è null.

Esempio

Nell'esempio di codice seguente viene assegnato un elenco di clienti all'oggetto DataSource di un BindingSource componente. Questo esempio di codice fa parte di un esempio più grande fornito in Procedura: Generare notifiche di modifica usando il metodo BindingSource ResetItem.

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

Commenti

La DataSource proprietà può essere impostata su una serie di origini dati, tra cui tipi, oggetti e elenchi di tipi. L'origine dati risultante verrà esposta come elenco. La tabella seguente mostra alcune delle origini dati comuni e la valutazione dell'elenco risultante.

DataSource, proprietà Risultati elenco
null Oggetto vuoto IBindingList di oggetti. L'aggiunta di un elemento imposta l'elenco al tipo dell'elemento aggiunto.
null con DataMember set Non supportato, genera ArgumentException.
Tipo o oggetto non elenco di tipo "T" Vuoto IBindingList di tipo "T".
Istanza di matrice IBindingList contenente gli elementi della matrice.
Istanza dell'interfaccia IEnumerable Oggetto IBindingList contenente gli IEnumerable elementi.
Istanza di elenco contenente il tipo "T" IBindingList istanza contenente il tipo "T".

Inoltre, DataSource può essere impostato su altri tipi di elenco, ad IListSource esempio e ITypedList e li BindingSource gestirà in modo appropriato. In questo caso, il tipo contenuto nell'elenco deve avere un costruttore senza parametri.

Quando si imposta un'origine dati, se il riferimento fornito contiene più di un elenco o una tabella, è necessario impostare la proprietà su una stringa che specifica l'elenco su cui eseguire l'associazione DataMember . L'impostazione di questa proprietà genera l'evento DataSourceChanged .

Nota

Se si apportano modifiche al valore della DataSource proprietà, è consigliabile farlo nel thread dell'interfaccia utente per assicurarsi che l'interfaccia utente rifletta le modifiche.

La DataSource proprietà è la proprietà predefinita per la BindingSource classe.

Si applica a

Vedi anche