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.

[Visual Basic]
Public Sub SetDataBinding( _
   ByVal dataSource As Object, _
   ByVal dataMember As String _
)
[C#]
public void SetDataBinding(
 object dataSource,
 string dataMember
);
[C++]
public: void SetDataBinding(
 Object* dataSource,
 String* dataMember
);
[JScript]
public function SetDataBinding(
   dataSource : Object,
 dataMember : String
);

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.

Exceptions

Exception Type Condition
ArgumentException One or more of the arguments are invalid.
ArgumentNullException The dataSource argument is a null reference (Nothing in Visual Basic).

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 allows users to edit data but prevents them from adding new rows by using a DataView as the data source and setting the AddNew property to false. When the DataSource is a DataView or DataTable, set the DataMember to an empty string ("").

Example

[Visual Basic, C#, C++] The following example sets the DataSource and DataMember to a DataSet, and a DataTable in the DataSet, respectively.

[Visual Basic] 
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

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

[C++] 
private:
void BindControls(){
    // Creates a DataSet named SuppliersProducts.
    DataSet* SuppliersProducts = new DataSet(S"SuppliersProducts");
    // Adds two DataTable objects, Suppliers and Products.
    SuppliersProducts->Tables->Add(new DataTable(S"Suppliers"));
    SuppliersProducts->Tables->Add(new DataTable(S"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, S"Suppliers");
}
   

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

DataGrid Class | DataGrid Members | System.Windows.Forms Namespace | DataMember | DataSource | DataSet | DataView

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft