DataGrid.DataSource Property


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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public object DataSource { get; set; }

Property Value

Type: System.Object

An object that functions as a data source.

At run time, use the SetDataBinding method to set the DataSource and DataMember properties.

The following data sources are valid:

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

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

Setting the DataSource to an object that does not implement the IList interface or an 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 DataView as the data source and setting the AddNew property to false.

To bind the DataGrid to a strongly typed array of objects, the object type must contain public properties. To create a 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 MappingName property is case-sensitive; the type name must be matched exactly. See the MappingName property for an example.

You can also bind the DataGrid to an ArrayList. A feature of the ArrayList is that it can contain objects of multiple types, but the 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 Control, the second item could be a TextBox (which inherits from Control). If, on the other hand, the first item is a TextBox, the second object cannot be a 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 MappingName of the DataGridTableStyle to "ArrayList" (the type name).

The following code example shows how to set the DataSource, and when needed, the DataMember, to bind a System.Windows.Forms.DataGrid to both a DataView and a DataSet. The example also shows how to return data sources from the System.Windows.Forms.DataGrid.

private void BindToDataView(DataGrid myGrid){
    // Create a DataView using the DataTable.
    DataTable myTable = new DataTable("Suppliers");
    // Insert code to create and populate columns.
    DataView myDataView = new DataView(myTable);
    myGrid.DataSource = myDataView;
 private void BindToDataSet(DataGrid myGrid){
    // Create a DataSet.
    DataSet myDataSet = new DataSet("myDataSet");
    // Insert code to populate DataSet with several tables.
    myGrid.DataSource = myDataSet;
    // Use the DataMember property to specify the DataTable.
    myGrid.DataMember = "Suppliers";
 private DataView GetDataViewFromDataSource(){
    // Create a DataTable variable, and set it to the DataSource.
    DataView myDataView;
    myDataView = (DataView) dataGrid1.DataSource;
    return myDataView;
 private DataSet GetDataSetFromDataSource(){
    // Create a DataSet variable, and set it to the DataSource.
    DataSet myDataSet;
    myDataSet = (DataSet) dataGrid1.DataSource;
    return myDataSet;

.NET Framework
Available since 1.1
Return to top