Export (0) Print
Expand All
Expand Minimize

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


Type: System.Object
The data source for the System.Windows.Forms.DataGrid control.
Type: System.String
The DataMember string that specifies the table to bind to within the object returned by the DataSource property.


One or more of the arguments are invalid.


The dataSource argument is null.

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");

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2015 Microsoft