Bagikan melalui


DataGrid.DataSource Properti

Definisi

Mendapatkan atau mengatur sumber data tempat kisi menampilkan data.

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

Nilai Properti

Objek yang berfungsi sebagai sumber data.

Atribut

Contoh

Contoh kode berikut menunjukkan cara mengatur DataSource, dan ketika diperlukan, DataMember, untuk mengikat System.Windows.Forms.DataGrid ke dan DataViewDataSet. Contohnya juga menunjukkan cara mengembalikan sumber data dari 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

Keterangan

Pada waktu proses, gunakan SetDataBinding metode untuk mengatur DataSource properti dan DataMember .

Sumber data berikut ini valid:

Binding Lihat gambaran umum kelas untuk informasi selengkapnya tentang sumber data.

DataSource Jika referensi berisi lebih dari satu tabel, Anda harus mengatur DataMember properti string yang menentukan tabel yang akan diikat. Misalnya, jika DataSource adalah DataSet atau DataViewManager yang berisi tiga tabel bernama Customers, , Ordersdan OrderDetails, Anda harus menentukan tabel yang akan diikat.

DataSource Mengatur ke objek yang tidak mengimplementasikan IList antarmuka atau akan IListSource menyebabkan kisi melemparkan pengecualian.

Anda dapat membuat kisi yang memungkinkan pengguna mengedit data tetapi mencegah mereka menambahkan baris baru dengan menggunakan DataView sebagai sumber data dan mengatur AddNew() properti ke false.

Untuk mengikat DataGrid ke array objek yang sangat ditik, jenis objek harus berisi properti publik. Untuk membuat DataGridTableStyle yang menampilkan array, atur DataGridTableStyle.MappingName properti ke typename tempat typename diganti dengan nama jenis objek. Perhatikan juga bahwa MappingName properti peka huruf besar/kecil; nama jenis harus dicocokkan dengan tepat. MappingName Lihat properti untuk contoh.

Anda juga dapat mengikat DataGrid ke ArrayList. Fiturnya ArrayList adalah dapat berisi objek dari beberapa jenis, tetapi DataGrid hanya dapat mengikat ke daftar seperti itu ketika semua item dalam daftar memiliki tipe yang sama dengan item pertama. Ini berarti bahwa semua objek harus memiliki jenis yang sama, atau harus mewarisi dari kelas yang sama dengan item pertama dalam daftar. Misalnya, jika item pertama dalam daftar adalah Control, item kedua bisa menjadi TextBox (yang mewarisi dari Control). Jika, di sisi lain, item pertama adalah TextBox, objek kedua tidak dapat berupa Control. Selanjutnya, ArrayList harus memiliki item di dalamnya ketika terikat. ArrayList Kosong akan menghasilkan kisi kosong. Selain itu, objek dalam ArrayList harus berisi properti publik. Saat mengikat ke ArrayList, atur MappingName dari DataGridTableStyle ke "ArrayList" (nama jenis).

Berlaku untuk

Lihat juga