DataGrid.DataSource 속성

정의

표에 표시되는 데이터의 대상 데이터 소스를 가져오거나 설정합니다.

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

속성 값

Object

데이터 소스로 작동하는 개체입니다.

특성

예제

다음 코드 예제에서는 a와 a 둘 다에 DataSource바인딩 System.Windows.Forms.DataGrid 하기 위해 , 및 필요한 DataMember경우를 설정하는 방법을 보여줍니다.DataView DataSet 이 예제에서는 에서 데이터 원본을 반환하는 방법도 보여 줍니다 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 사용하여 속성과 DataMember 속성을 설정합니다DataSource.

다음 데이터 원본은 유효합니다.

데이터 원본에 Binding 대한 자세한 내용은 클래스 개요를 참조하세요.

참조에 DataSource 둘 이상의 테이블이 포함된 경우 바인딩할 테이블을 지정하는 문자열로 속성을 설정 DataMember 해야 합니다. 예를 들어 이름이 3OrderDetails``Orders``Customers개인 테이블 또는 DataViewManager DataSet 테이블이 있는 경우 DataSource 바인딩할 테이블을 지정해야 합니다.

DataSource 인터페이스를 구현 IList 하지 않는 개체 또는 IListSource 개체로 설정하면 그리드에서 예외가 throw됩니다.

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.

강력한 형식의 개체 배열에 바인딩 DataGrid 하려면 개체 형식에 공용 속성이 포함되어야 합니다. 배열을 DataGridTableStyle 표시하는 속성을 만들려면 개체 형식의 이름으로 대체되는 위치 typename 로 속성을 typename 설정합니다DataGridTableStyle.MappingName. 또한 속성은 MappingName 대/소문자를 구분합니다. 형식 이름은 정확히 일치해야 합니다. 예제는 MappingName 속성을 참조하세요.

에 바인딩할 DataGrid ArrayList수도 있습니다. 여러 형식의 개체를 포함할 수 있지만 DataGrid 목록의 ArrayList 모든 항목이 첫 번째 항목과 동일한 형식인 경우에만 이러한 목록에 바인딩할 수 있습니다. 즉, 모든 개체는 동일한 형식이거나 목록의 첫 번째 항목과 동일한 클래스에서 상속되어야 합니다. 예를 들어 목록의 첫 번째 항목이 a Control인 경우 두 번째 항목은 상속되는 항목일 TextBox 수 있습니다 Control. 반면에 첫 번째 항목이 TextBox이면 두 번째 개체가 될 Control수 없습니다. 또한 바인딩된 ArrayList 경우 항목이 있어야 합니다. 비어 ArrayList 있는 경우 빈 표가 생성됩니다. 또한 해당 개체에는 공용 속성이 ArrayList 포함되어야 합니다. 바인딩할 ArrayList때 "ArrayList"(형식 이름)로 설정합니다 MappingName DataGridTableStyle .

적용 대상

추가 정보