DataGrid.SetDataBinding(Object, String) 方法

定義

設定執行階段時的 DataSourceDataMember 屬性。

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)

參數

dataSource
Object

DataGrid 控制項的資料來源。

dataMember
String

DataMember 字串,指定 DataSource 屬性傳回物件中繫結的資料表。

例外狀況

一或多個引數無效。

dataSource 引數為 null

範例

下列程式碼範例會將 和 DataMember 分別設定 DataSourceDataSet 中的 DataSetDataTable

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

備註

您必須在執行時間使用 SetDataBinding 方法,才能重設 DataSource 屬性。

DataSource如需設定有效資料來源的詳細資訊,請參閱 屬性。

您可以建立一個方格,讓使用者編輯資料,但防止他們新增資料列,方法是使用 DataView 做為資料來源,並將 屬性設定 AllowNewfalseDataSource當 是 DataViewDataTable 時,請將 設定 DataMember 為空字串 (「」) 。

適用於

另請參閱