System.Windows.Forms.DataGrid.DataSource Property

Gets or sets the data source that the grid is displaying data for.

Syntax

[System.ComponentModel.AttributeProvider(typeof(System.ComponentModel.IListSource))]
[System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
[System.ComponentModel.DefaultValue(null)]
public object DataSource { set; get; }

Value

Documentation for this section has not yet been entered.

Remarks

At run time, use the DataGrid.SetDataBinding(object, string) method to set the DataGrid.DataSource and DataGrid.DataMember properties.

The following data sources are valid:

See the System.Windows.Forms.Binding class overview for more information on data sources.

If the DataGrid.DataSource reference contains more than one table, you must set the DataGrid.DataMember property a string that specifies the table to bind to. For example, if the DataGrid.DataSource is a System.Data.DataSet or System.Data.DataViewManager that contains three tables named Customers, Orders, and OrderDetails, you must specify the table to bind to.

Setting the DataGrid.DataSource to an object that does not implement the IList interface or an System.ComponentModel.IListSource will cause the grid to throw an exception.

You can create a grid that enables users to edit data but prevents them from adding new rows by using a System.Data.DataView as the data source and setting the System.Data.DataView.AddNew property to false.

To bind the System.Windows.Forms.DataGrid to a strongly typed array of objects, the object type must contain public properties. To create a System.Windows.Forms.DataGridTableStyle that displays the array, set the DataGridTableStyle.MappingName property to typename where typename is replaced by the name of the object type. Also note that the DataGridTableStyle.MappingName property is case-sensitive; the type name must be matched exactly. See the DataGridTableStyle.MappingName property for an example.

You can also bind the System.Windows.Forms.DataGrid to an ArrayList. A feature of the ArrayList is that it can contain objects of multiple types, but the System.Windows.Forms.DataGrid can only bind to such a list when all items in the list are of the same type as the first item. This means that all objects must either be of the same type, or they must inherit from the same class as the first item in the list. For example, if the first item in a list is a System.Windows.Forms.Control, the second item could be a System.Windows.Forms.TextBox (which inherits from System.Windows.Forms.Control). If, on the other hand, the first item is a System.Windows.Forms.TextBox, the second object cannot be a System.Windows.Forms.Control. Further, the ArrayList must have items in it when it is bound. An empty ArrayList will result in an empty grid. In addition, the objects in the ArrayList must contain public properties. When binding to an ArrayList, set the DataGridTableStyle.MappingName of the System.Windows.Forms.DataGridTableStyle to "ArrayList" (the type name).

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0