DataGrid.DataSource 属性

定义

获取或设置网格所显示数据的数据源。Gets or sets the data source that the grid is displaying data for.

public:
 property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public object DataSource { get; set; }
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public object DataSource { get; set; }
member this.DataSource : obj with get, set
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DataSource : obj with get, set
Public Property DataSource As Object

属性值

Object

作为数据源的对象。An object that functions as a data source.

属性

示例

下面的代码示例演示如何设置和( DataSource 如果需要),以将 DataMember 绑定 System.Windows.Forms.DataGridDataViewDataSetThe 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. 该示例还演示如何从返回数据源 System.Windows.Forms.DataGridThe 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 = gcnew DataTable( "Suppliers" );
      // Insert code to create and populate columns.
      DataView^ myDataView = gcnew DataView( myTable );
      myGrid->DataSource = myDataView;
   }

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

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

   DataSet^ GetDataSetFromDataSource()
   {
      // Create a DataSet variable, and set it to the DataSource.
      DataSet^ myDataSet;
      myDataSet = (DataSet^)(dataGrid1->DataSource);
      return myDataSet;
   }
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;
 }

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

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

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

注解

在运行时,使用 SetDataBinding 方法来设置 DataSourceDataMember 属性。At run time, use the SetDataBinding method to set the DataSource and DataMember properties.

以下数据源有效:The following data sources are valid:

Binding有关数据源的详细信息,请参阅类概述。See the Binding class overview for more information on data sources.

如果 DataSource 引用包含多个表,则必须将属性设置为 DataMember 指定要绑定到的表的字符串。If the DataSource reference contains more than one table, you must set the DataMember property a string that specifies the table to bind to. 例如,如果 DataSource 是一个 DataSetDataViewManager 包含三个名为、和的表, Customers Orders OrderDetails 则必须指定要绑定到的表。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.

如果将设置 DataSource 为不实现接口的对象,则会 IList IListSource 导致网格引发异常。Setting the DataSource to an object that does not implement the IList interface or an IListSource will cause the grid to throw an exception.

您可以创建一个网格,使用户能够编辑数据,但通过使用 DataView 作为数据源并将属性设置为来阻止用户添加新行 AddNew falseYou 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.

若要将绑定 DataGrid 到对象的强类型数组,对象类型必须包含公共属性。To bind the DataGrid to a strongly typed array of objects, the object type must contain public properties. 若要创建 DataGridTableStyle 显示数组的,请将属性设置为,其中,将 DataGridTableStyle.MappingName typename typename 替换为对象类型的名称。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. 另请注意, MappingName 属性区分大小写; 类型名称必须完全匹配。Also note that the MappingName property is case-sensitive; the type name must be matched exactly. 有关示例,请参见 MappingName 属性。See the MappingName property for an example.

你还可以将绑定 DataGridArrayListYou can also bind the DataGrid to an ArrayList. 的一项功能 ArrayList 是它可以包含多种类型的对象,但 DataGrid 如果列表中的所有项的类型与第一个项的类型相同,则只能绑定到此类列表。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. 例如,如果列表中的第一项是 Control ,则第二项可以是 TextBox 继承自) 的 (ControlFor example, if the first item in a list is a Control, the second item could be a TextBox (which inherits from Control). 另一方面,如果第一项为 TextBox ,则第二个对象不能为 ControlIf, on the other hand, the first item is a TextBox, the second object cannot be a Control. 此外, ArrayList 当绑定时,必须具有其中的项。Further, the ArrayList must have items in it when it is bound. 如果为空, ArrayList 则将生成一个空网格。An empty ArrayList will result in an empty grid. 此外,中的对象 ArrayList 必须包含公共属性。In addition, the objects in the ArrayList must contain public properties. 绑定到时 ArrayList ,将的设置 MappingName DataGridTableStyle 为 "ArrayList" (类型名称) 。When binding to an ArrayList, set the MappingName of the DataGridTableStyle to "ArrayList" (the type name).

适用于

另请参阅