BindingSource.DataSource Vlastnost

Definice

Získá nebo nastaví zdroj dat, se kterým konektor vytvoří vazbu.

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

Hodnota vlastnosti

Objekt Object , který funguje jako zdroj dat. Výchozí formát je null.

Příklady

Následující příklad kódu přiřadí seznam zákazníků komponentě DataSourceBindingSource . Tento příklad kódu je součástí většího příkladu, který najdete v tématu Postupy: Vyvolání oznámení o změnách pomocí metody 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

Poznámky

Vlastnost DataSource může být nastavena na řadu zdrojů dat, včetně typů, objektů a seznamů typů. Výsledný zdroj dat se zobrazí jako seznam. Následující tabulka ukazuje některé z běžných zdrojů dat a výsledné vyhodnocení seznamu.

Vlastnost DataSource Výpis výsledků
null Prázdné IBindingList objekty. Přidání položky nastaví seznam na typ přidané položky.
null se DataMember sadou Nepodporuje se, vyvolá ArgumentException.
Typ nebo objekt typu T, který není seznamem Prázdný IBindingList typ "T".
Instance pole IBindingList obsahující prvky pole.
IEnumerable Instance IEnumerable Obsahuje IBindingList položky.
Seznam instancí obsahující typ "T" IBindingList instance obsahující typ "T".

Kromě toho lze nastavit na jiné typy seznamů, DataSource jako IListSource jsou a ITypedList , a BindingSource bude je správně zpracovávat. V tomto případě by typ obsažený v seznamu měl mít konstruktor bez parametrů.

Pokud při nastavování zdroje dat obsahuje zadaný odkaz více než jeden seznam nebo tabulku, musíte vlastnost nastavit DataMember na řetězec, který určuje seznam, se kterým se má vytvořit vazba. Nastavení této vlastnosti vyvolá DataSourceChanged událost.

Poznámka

Pokud provedete jakékoli změny DataSource hodnoty vlastnosti, měli byste to udělat ve vlákně uživatelského rozhraní, abyste zajistili, že uživatelské rozhraní změny odráží.

Vlastnost DataSource je výchozí vlastností třídy BindingSource .

Platí pro

Viz také