DataGrid.SetDataBinding(Object, String) Método

Definição

Define as propriedades DataSource e DataMember no 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 para o controle DataGrid.

dataMember
String

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

Exceções

Um ou mais argumentos são inválidos.

O argumento dataSource é null.

Exemplos

O exemplo de código a seguir define o DataSource e como um DataSete 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 que os usuários editem dados, mas os impede de adicionar novas linhas usando um DataView como a fonte de dados e definindo a AllowNew propriedade falsecomo . Quando o DataSource for um DataView ou DataTable, defina o DataMember como uma cadeia de caracteres vazia ("").

Aplica-se a

Confira também