DataGrid.SetDataBinding(Object, String) 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在运行时设置 DataSource 和 DataMember 属性。
public:
 void SetDataBinding(System::Object ^ dataSource, System::String ^ dataMember);public void SetDataBinding (object dataSource, string dataMember);member this.SetDataBinding : obj * string -> unitPublic Sub SetDataBinding (dataSource As Object, dataMember As String)参数
- dataMember
- String
DataMember 字符串,它指定该表要绑定到 DataSource 属性所返回的对象中。
例外
一个或多个参数无效。
dataSource 参数为 null。
示例
下面的代码示例分别设置 DataSource 和 DataMember 设置为 DataSeta 和 a DataTable 和 a DataSet。
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数据源并将属性false设置为 AllowNew 。 DataSource如果为DataView或DataTable,则设置为DataMember空字符串 (“”) 。