DataGrid.SetDataBinding(Object, String) Method

Definition

Sets the DataSource and DataMember properties at run time.

C#
public void SetDataBinding(object dataSource, string dataMember);

Parameters

dataSource
Object

The data source for the DataGrid control.

dataMember
String

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

Exceptions

One or more of the arguments are invalid.

The dataSource argument is null.

Examples

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

C#
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");
 }

Remarks

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 ("").

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

See also