Share via


DataGrid.SetDataBinding 方法

在运行时设置 DataSourceDataMember 属性。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Sub SetDataBinding ( _
    dataSource As Object, _
    dataMember As String _
)
用法
Dim instance As DataGrid
Dim dataSource As Object
Dim dataMember As String

instance.SetDataBinding(dataSource, dataMember)
public void SetDataBinding (
    Object dataSource,
    string dataMember
)
public:
void SetDataBinding (
    Object^ dataSource, 
    String^ dataMember
)
public void SetDataBinding (
    Object dataSource, 
    String dataMember
)
public function SetDataBinding (
    dataSource : Object, 
    dataMember : String
)

参数

  • dataMember
    DataMember 字符串,它指定该表要绑定到 DataSource 属性所返回的对象中。

异常

异常类型 条件

ArgumentException

一个或多个参数无效。

ArgumentNullException

dataSource 参数为 空引用(在 Visual Basic 中为 Nothing)。

备注

在运行时必须使用 SetDataBinding 方法来重置 DataSource 属性。

有关设置有效数据源的更详细信息,请参见 DataSource 属性。

通过将 DataView 用作数据源并将 AllowNew 属性设置为 false,可以创建网格,该网格允许用户编辑数据但禁止他们添加新行。当 DataSourceDataViewDataTable 时,请将 DataMember 设置成空字符串 ("")。

示例

下面的代码示例将 DataSourceDataMember 分别设置为 DataSet 中的 DataSetDataTable

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 'BindControls
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:
   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.get_Tables().Add(new DataTable("Suppliers"));
    suppliersProducts.get_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");
} //BindControls

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

DataGrid 类
DataGrid 成员
System.Windows.Forms 命名空间
DataMember
DataSource
DataSet
DataView