Partilhar via


BindingSource.DataSource Propriedade

Definição

Obtém ou define a fonte de dados à qual o conector se associa.

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

Valor da propriedade

Um Object que atua como uma fonte de dados. O padrão é null.

Exemplos

O exemplo de código a seguir atribui uma lista de clientes ao DataSource componente BindingSource . Este exemplo de código faz parte de um exemplo maior fornecido em How to: Raise Change Notifications Using the BindingSource ResetItem Method.

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

Comentários

A DataSource propriedade pode ser definida como várias fontes de dados, incluindo tipos, objetos e listas de tipos. A fonte de dados resultante será exposta como uma lista. A tabela a seguir mostra algumas das fontes de dados comuns e a avaliação da lista resultante.

Propriedade DataSource Listar resultados
null Um vazio IBindingList de objetos. Adicionar um item define a lista para o tipo do item adicionado.
null com DataMember set Sem suporte, gera ArgumentException.
Tipo ou objeto não de lista do tipo "T" Vazio IBindingList do tipo "T".
Instância de matriz IBindingList que contém os elementos da matriz.
IEnumerable Instância Um IBindingList que contém os IEnumerable itens.
Listar instância que contém o tipo "T" IBindingList instância que contém o tipo "T".

Além disso, DataSource pode ser definido como outros tipos de lista, como IListSource e ITypedList eles BindingSource serão tratados adequadamente. Nesse caso, o tipo contido na lista deve ter um construtor sem parâmetros.

Ao definir uma fonte de dados, se a referência fornecida contiver mais de uma lista ou tabela, você deverá definir a DataMember propriedade como uma cadeia de caracteres que especifica a lista à qual associar. Definir essa propriedade gera o DataSourceChanged evento.

Observação

Se você fizer alterações no valor da DataSource propriedade, deverá fazer isso no thread da interface do usuário para garantir que a interface do usuário reflita as alterações.

A DataSource propriedade é a propriedade padrão para a BindingSource classe.

Aplica-se a

Confira também