BindingSource.DataSource Egenskap

Definition

Hämtar eller anger den datakälla som anslutningsappen binder till.

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

Egenskapsvärde

En Object som fungerar som en datakälla. Standardvärdet är null.

Exempel

I följande kodexempel tilldelas en lista över kunder till DataSource en BindingSource komponent. Det här kodexemplet är en del av ett större exempel på How to: Raise Change Notifications Using the BindingSource ResetItem Method (Skapa ändringsmeddelanden med metoden 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

Kommentarer

Egenskapen DataSource kan anges till ett antal datakällor, inklusive typer, objekt och listor med typer. Den resulterande datakällan visas som en lista. I följande tabell visas några av de vanliga datakällorna och den resulterande listutvärderingen.

DataSource-egenskap Lista de resultat
null Ett tomt IBindingList objekt. Genom att lägga till ett objekt ändras listans typ till typen på det tillagda objektet.
null med DataMember inställd Stöds inte, genererar ArgumentException.
Icke-listtyp eller objekt av typen "T" Tom IBindingList av typen "T".
Arrayinstans IBindingList innehåller matriselementen.
IEnumerable instans En IBindingList som innehåller objekten IEnumerable .
Listinstans som innehåller typen "T" IBindingList instans som innehåller typen "T".

Dessutom DataSource kan ställas in på andra listtyper, till exempel IListSource och ITypedList och BindingSource kommer att hantera dem på rätt sätt. I det här fallet ska typen i listan ha en parameterlös konstruktor.

Om den angivna referensen innehåller mer än en lista eller tabell när du anger en datakälla måste du ange DataMember egenskapen till en sträng som anger vilken lista som ska bindas till. Om du anger den här egenskapen genereras DataSourceChanged händelsen.

Note

Om du gör några ändringar i egenskapsvärdet DataSource bör du göra det i användargränssnittstråden (UI) för att säkerställa att användargränssnittet återspeglar ändringarna.

Egenskapen DataSource är standardegenskapen BindingSource för klassen.

Gäller för

Se även