DataGrid.SetDataBinding(Object, String) Metoda

Definicja

DataSource Ustawia właściwości i DataMember w czasie wykonywania.

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)

Parametry

dataSource
Object

Źródło danych dla kontrolki DataGrid .

dataMember
String

Ciąg DataMember określający tabelę, która ma być powiązana z obiektem zwróconym DataSource przez właściwość .

Wyjątki

Co najmniej jeden argument jest nieprawidłowy.

Argumentem dataSource jest null.

Przykłady

Poniższy przykład kodu ustawia DataSource odpowiednio elementy i DataMember na DataSet, i w DataTableDataSetobiekcie .

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

Uwagi

Aby zresetować DataSource właściwość, należy użyć SetDataBinding metody w czasie wykonywania.

Zobacz właściwość , DataSource aby uzyskać więcej informacji na temat ustawiania prawidłowego źródła danych.

Możesz utworzyć siatkę, która umożliwia użytkownikom edytowanie danych, ale uniemożliwia dodawanie nowych wierszy przy użyciu elementu DataView jako źródła danych i ustawiania AllowNew właściwości na falsewartość . Gdy parametr DataSource ma wartość DataView lub DataTable, ustaw DataMember wartość na pusty ciąg ("").

Dotyczy

Zobacz też