BindingSource.DataSource Właściwość

Definicja

Pobiera lub ustawia źródło danych powiązane z łącznikiem.

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

Wartość właściwości

Element Object , który działa jako źródło danych. Wartość domyślna to null.

Przykłady

Poniższy przykład kodu przypisuje listę klientów do DataSourceBindingSource składnika. Ten przykład kodu jest częścią większego przykładu przedstawionego w sekcji Instrukcje: zgłaszanie powiadomień o zmianie przy użyciu 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

Uwagi

Właściwość DataSource można ustawić na wiele źródeł danych, w tym typy, obiekty i listy typów. Wynikowe źródło danych zostanie uwidocznione jako lista. W poniższej tabeli przedstawiono niektóre typowe źródła danych i wynikową ocenę listy.

Właściwość DataSource Wyświetlanie listy wyników
null IBindingList Pusty obiekt. Dodanie elementu powoduje ustawienie listy na typ dodanego elementu.
nullz zestawem DataMember Nieobsługiwane, zgłasza wartość ArgumentException.
Typ innej niż lista lub obiekt typu "T" Pusty IBindingList typ "T".
Wystąpienie tablicy IBindingList zawierające elementy tablicy.
IEnumerable Wystąpienie Element IBindingList zawierający IEnumerable elementy.
Wystąpienie listy zawierające typ "T" IBindingList wystąpienie zawierające typ "T".

Ponadto można ustawić na inne typy list, DataSource takie jak IListSource i ITypedList , i BindingSource będzie je odpowiednio obsługiwać. W takim przypadku typ zawarty na liście powinien mieć konstruktor bez parametrów.

Podczas ustawiania źródła danych, jeśli podane odwołanie zawiera więcej niż jedną listę lub tabelę, należy ustawić DataMember właściwość na ciąg określający listę, z którą ma zostać powiązana. Ustawienie tej właściwości powoduje DataSourceChanged podniesienie zdarzenia.

Uwaga

Jeśli wprowadzisz jakiekolwiek zmiany w DataSource wartości właściwości, należy to zrobić w wątku interfejsu użytkownika, aby upewnić się, że interfejs użytkownika odzwierciedla zmiany.

Właściwość DataSource jest właściwością domyślną BindingSource klasy.

Dotyczy

Zobacz też