Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataGrid.SetDataBinding Method

Sets the DataSource and DataMember properties at run time.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public void SetDataBinding (
	Object dataSource,
	string dataMember
)
public void SetDataBinding (
	Object dataSource, 
	String dataMember
)
public function SetDataBinding (
	dataSource : Object, 
	dataMember : String
)
Not applicable.

Parameters

dataSource

The data source for the System.Windows.Forms.DataGrid control.

dataMember

The DataMember string that specifies the table to bind to within the object returned by the DataSource property.

Exception typeCondition

ArgumentException

One or more of the arguments are invalid.

ArgumentNullException

The dataSource argument is a null reference (Nothing in Visual Basic).

You must use the SetDataBinding method at run time to reset the DataSource property.

See the DataSource property for more details about setting a valid data source.

You can create a grid that enables users to edit data but prevents them from adding new rows by using a DataView as the data source and setting the AllowNew property to false. When the DataSource is a DataView or DataTable, set the DataMember to an empty string ("").

The following code example sets the DataSource and DataMember to a DataSet, and a DataTable in the DataSet, respectively.

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 = 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 Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

Show:
© 2014 Microsoft