Partilhar via


DataGrid.SetDataBinding(Object, String) Método

Definição

Define as propriedades e DataMember as DataSource propriedades em tempo de execução.

public:
 void SetDataBinding(System::Object ^ dataSource, System::String ^ dataMember);
public void SetDataBinding(object dataSource, string dataMember);
member this.SetDataBinding : obj * string -> unit
Public Sub SetDataBinding (dataSource As Object, dataMember As String)

Parâmetros

dataSource
Object

A fonte de dados do DataGrid controle.

dataMember
String

A DataMember cadeia de caracteres que especifica a tabela a ser associada dentro do objeto retornado pela DataSource propriedade.

Exceções

Um ou mais dos argumentos são inválidos.

O dataSource argumento é null.

Exemplos

O exemplo de código a seguir define o DataSource e para um DataSet, e um DataTable no DataSetDataMember , respectivamente.

private:
   void BindControls()
   {
      // Creates a DataSet named SuppliersProducts.
      DataSet^ SuppliersProducts = gcnew DataSet( "SuppliersProducts" );
      // Adds two DataTable objects, Suppliers and Products.
      SuppliersProducts->Tables->Add( gcnew DataTable( "Suppliers" ) );
      SuppliersProducts->Tables->Add( gcnew DataTable( "Products" ) );
      // Insert code to add DataColumn objects.
      // Insert code to fill tables with columns and data.
      // Binds the DataGrid to the DataSet, displaying the Suppliers table.
      dataGrid1->SetDataBinding( SuppliersProducts, "Suppliers" );
   }
private void BindControls(){
    // Creates a DataSet named SuppliersProducts.
    DataSet SuppliersProducts = new DataSet("SuppliersProducts");
    // Adds two DataTable objects, Suppliers and Products.
    SuppliersProducts.Tables.Add(new DataTable("Suppliers"));
    SuppliersProducts.Tables.Add(new DataTable("Products"));
    // Insert code to add DataColumn objects.
    // Insert code to fill tables with columns and data.
    // Binds the DataGrid to the DataSet, displaying the Suppliers table.
    dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers");
 }
Private Sub BindControls()
    ' Create a DataSet named SuppliersProducts.
    Dim SuppliersProducts As New DataSet("SuppliersProducts")
    ' Adds two DataTable objects, Suppliers and Products.
    SuppliersProducts.Tables.Add(New DataTable("Suppliers"))
    SuppliersProducts.Tables.Add(New DataTable("Products"))
    ' Insert code to add DataColumn objects.
    ' Insert code to fill tables with columns and data.
    ' Binds the DataGrid to the DataSet, displaying the Suppliers table.
    dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers")
End Sub

Comentários

Você deve usar o SetDataBinding método em tempo de execução para redefinir a DataSource propriedade.

Consulte a DataSource propriedade para obter mais detalhes sobre como definir uma fonte de dados válida.

Você pode criar uma grade que permite aos usuários editar dados, mas impede que eles adicionem novas linhas usando uma DataView como fonte de dados e definindo a AllowNew propriedade como false. Quando for DataSource um DataView ou DataTable, defina como DataMember uma cadeia de caracteres vazia ("").

Aplica-se a

Confira também