DataGrid.DataSource 属性

获取或设置网格所显示数据的数据源。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property DataSource As Object
用法
Dim instance As DataGrid
Dim value As Object

value = instance.DataSource

instance.DataSource = value
public Object DataSource { get; set; }
public:
property Object^ DataSource {
    Object^ get ();
    void set (Object^ value);
}
/** @property */
public Object get_DataSource ()

/** @property */
public void set_DataSource (Object value)
public function get DataSource () : Object

public function set DataSource (value : Object)

属性值

作为数据源的对象。

备注

在运行时使用 SetDataBinding 方法来设置 DataSourceDataMember 属性。

下列数据源有效:

有关数据源的更多信息,请参见 Binding 类的概述。

如果 DataSource 引用包含的表不止一个,则必须向 DataMember 属性设置一个字符串,该字符串指定要绑定到的表。例如,如果 DataSource 是包含名为 CustomersOrdersOrderDetails 的三个表的 DataSetDataViewManager,则必须指定要绑定的表。

DataSource 设置为一个不实现 IList 接口的对象或者一个 IListSource 会导致网格发生异常。

通过将 DataView 用作数据源并将 AddNew 属性设置为 false,可以创建网格,该网格允许用户编辑数据但禁止他们添加新行。

若要将 DataGrid 绑定到对象的强类型数组,该对象类型必须包含公共属性。若要创建一个显示此数组的 DataGridTableStyle,请将 DataGridTableStyle.MappingName 属性设置为 typename,其中 typename 由该对象类型的名称替换。另外请注意,MappingName 属性区分大小写;类型名称必须完全匹配。有关示例,请参见 MappingName 属性。

也可以将 DataGrid 绑定到 ArrayListArrayList 的一个功能是它可以包含多种类型的对象,但当列表中的所有项与第一项具有相同的类型时,DataGrid 只能绑定到这类列表。这意味着所有的对象必须是同一种类型,或者必须从与列表中第一项相同的类继承。例如,如果列表中的第一项为 Control,则第二项可能为 TextBox(它从 Control 继承)。另一方面,如果第一项为 TextBox,则第二个对象就不可能是 Control。此外,ArrayList 在绑定时必须包含项目。空 ArrayList 会导致空网格。此外,ArrayList 中的对象必须包含公共属性。当绑定到 ArrayList 时,请将 DataGridTableStyleMappingName 设置为“ArrayList”(类型名)。

示例

下面的代码示例演示如何设置 DataSourceDataMember(需要时),以便将 System.Windows.Forms.DataGrid 同时绑定到 DataViewDataSet。该示例还显示如何从 System.Windows.Forms.DataGrid 返回数据源。

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
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:
   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.set_DataSource(myDataView);
} //BindToDataView

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

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

private DataSet GetDataSetFromDataSource()
{
    // Create a DataSet variable, and set it to the DataSource.
    DataSet myDataSet;
    myDataSet = ((DataSet)(dataGrid1.get_DataSource()));
    return myDataSet;
} //GetDataSetFromDataSource

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataGrid 类
DataGrid 成员
System.Windows.Forms 命名空间
DataGrid.DataMember 属性
DataSet
DataViewManager
DataView