This documentation is archived and is not being maintained.

DataGrid.DataSource Property

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

[Visual Basic]
Public Property DataSource As Object
[C#]
public object DataSource {get; set;}
[C++]
public: __property Object* get_DataSource();
public: __property void set_DataSource(Object*);
[JScript]
public function get DataSource() : Object;
public function set DataSource(Object);

Property Value

An object that functions as a data source.

Remarks

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 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.

To bind the DataGrid to a strongly typed array of objects, the object must contain public properties. To create a DataGridTableStyle that displays such an array, set the MappingName property to classname[] where classname is replaced by the class name. Also note that the MappingName property is case-sensitive. 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. When binding to an ArrayList, set the MappingName of the DataGridTableStyle to "ArrayList" (the type name).

Example

[Visual Basic, C#, C++] The following 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.

[Visual Basic] 
Private Sub BindToDataView(myGrid As DataGrid)
    ' Create a DataView using the DataTable.
    Dim myTable As New DataTable("Suppliers")
    ' Insert code to create and populate columns.
    Dim myDatatView As New DataView(myTable)
    myGrid.DataSource = myDatatView
End Sub 'BindToDataView

Private Sub BindToDataSet(myGrid As DataGrid)
    ' Create a DataSet.
    Dim myDataSet As 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"
End Sub 'BindToDataSet

Private Function GetDataViewFromDataSource() As DataView
    ' Create a DataTable variable, and set it to the DataSource.
    Dim myDatatView As DataView
    myDatatView = CType(dataGrid1.DataSource, DataView)
    Return myDatatView
End Function 'GetDataViewFromDataSource

Private Function GetDataSetFromDataSource() As DataSet
    ' Create a DataSet variable, and set it to the DataSource.
    Dim myDataSet As DataSet
    myDataSet = CType(dataGrid1.DataSource, DataSet)
    Return myDataSet
End Function 'GetDataSetFromDataSource

[C#] 
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;
 }


[C++] 
private:
 void BindToDataView(DataGrid* myGrid){
    // Create a DataView using the DataTable.
    DataTable* myTable = new DataTable(S"Suppliers");
    // Insert code to create and populate columns.
    DataView* myDataView = new DataView(myTable);
    myGrid->DataSource = myDataView;
 }

 void BindToDataSet(DataGrid* myGrid){
    // Create a DataSet.
    DataSet* myDataSet = new DataSet(S"myDataSet");
    // Insert code to populate DataSet with several tables.
    myGrid->DataSource = myDataSet;
    // Use the DataMember property to specify the DataTable.
    myGrid->DataMember = S"Suppliers";
 }

 DataView* GetDataViewFromDataSource(){
    // Create a DataTable variable, and set it to the DataSource.
    DataView* myDataView;
    myDataView = dynamic_cast<DataView*> (dataGrid1->DataSource);
    return myDataView;
 }

 DataSet* GetDataSetFromDataSource(){
    // Create a DataSet variable, and set it to the DataSource.
    DataSet* myDataSet;
    myDataSet = dynamic_cast<DataSet*> (dataGrid1->DataSource);
    return myDataSet;
 }

[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, .NET Compact Framework

See Also

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

Show: