Sdílet prostřednictvím


BindingSource.DataSource Vlastnost

Definice

Získá nebo nastaví zdroj dat, ke kterému 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

To Object funguje jako zdroj dat. Výchozí hodnota 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ý je k dispozici v části Postupy: Vyvolání oznámení o změnách pomocí BindingSource ResetItem – metoda.

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 lze nastavit na řadu zdrojů dat, včetně typů, objektů a seznamů typů. Výsledný zdroj dat se zobrazí jako seznam. Následující tabulka uvádí některé běžné zdroje 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ává ArgumentException.
Typ seznamu nebo objekt typu "T" Prázdný IBindingList typ "T".
Instance pole IBindingList obsahující prvky pole.
IEnumerable Instance IEnumerable Obsahuje IBindingList položky.
Výpis instance obsahující typ "T" IBindingList instance obsahující typ "T".

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

Při nastavování zdroje dat, pokud zadaný odkaz obsahuje více než jeden seznam nebo tabulku, je nutné nastavit DataMember vlastnost na řetězec, který určuje seznam pro vazbu. Nastavení této vlastnosti vyvolá DataSourceChanged událost.

Poznámka:

Pokud v hodnotě DataSource vlastnosti provedete nějaké změny, 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é