BindingSource.DataSource Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lekéri vagy beállítja azt az adatforrást, amelyhez az összekötő kapcsolódik.
public:
property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public object DataSource { get; set; }
public object? DataSource { get; set; }
member this.DataSource : obj with get, set
Public Property DataSource As Object
Tulajdonság értéke
Olyan Object , amely adatforrásként működik. Az alapértelmezett érték a null.
Példák
Az alábbi példakód egy ügyféllistát rendel egy DataSourceBindingSource összetevőhöz. Ez a példakód egy nagyobb példa része, amely a How to: Raise Change Notifications using the BindingSource ResetItem metódussal érhető el.
void Form1_Load( Object^ /*sender*/, EventArgs^ /*e*/ )
{
// Create and populate the list of DemoCustomer objects
// which will supply data to the DataGridView.
List< DemoCustomer^ >^ customerList = gcnew List< DemoCustomer^ >;
customerList->Add( DemoCustomer::CreateNewCustomer() );
customerList->Add( DemoCustomer::CreateNewCustomer() );
customerList->Add( DemoCustomer::CreateNewCustomer() );
// Bind the list to the BindingSource.
this->customersBindingSource->DataSource = customerList;
// Attach the BindingSource to the DataGridView.
this->customersDataGridView->DataSource =
this->customersBindingSource;
}
private void Form1_Load(System.Object sender, System.EventArgs e)
{
// Create and populate the list of DemoCustomer objects
// which will supply data to the DataGridView.
List<DemoCustomer> customerList = new List<DemoCustomer>();
customerList.Add(DemoCustomer.CreateNewCustomer());
customerList.Add(DemoCustomer.CreateNewCustomer());
customerList.Add(DemoCustomer.CreateNewCustomer());
// Bind the list to the BindingSource.
this.customersBindingSource.DataSource = customerList;
// Attach the BindingSource to the DataGridView.
this.customersDataGridView.DataSource =
this.customersBindingSource;
}
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Me.Load
' Create and populate the list of DemoCustomer objects
' which will supply data to the DataGridView.
Dim customerList As List(Of DemoCustomer) = _
New List(Of DemoCustomer)
customerList.Add(DemoCustomer.CreateNewCustomer())
customerList.Add(DemoCustomer.CreateNewCustomer())
customerList.Add(DemoCustomer.CreateNewCustomer())
' Bind the list to the BindingSource.
Me.customersBindingSource.DataSource = customerList
' Attach the BindingSource to the DataGridView.
Me.customersDataGridView.DataSource = Me.customersBindingSource
End Sub
Megjegyzések
A DataSource tulajdonság számos adatforrásra állítható be, beleértve a típusokat, objektumokat és típuslistákat. Az eredményként kapott adatforrás listaként lesz közzétéve. Az alábbi táblázat néhány gyakori adatforrást és az eredményként kapott listaértékelést mutatja be.
| DataSource tulajdonság | Találatok listázása |
|---|---|
null |
Üres IBindingList objektumokból. Ha hozzáad egy elemet, a lista a hozzáadott elem típusához lesz adva. |
null
DataMember készlettel |
Nem támogatott, emel ArgumentException. |
| Nem listatípus vagy "T" típusú objektum | "T" típusú üres IBindingList . |
| Tömbpéldány | IBindingList tartalmazza a tömbelemeket. |
| IEnumerable példány | Az IBindingList elemeket tartalmazó IEnumerable . |
| Példány, amely tartalmazza a "T" típusú listát | IBindingList "T" típusú példány. |
Emellett más listatípusokra is beállítható, DataSource például IListSource és ITypedList a BindingSource megfelelő módon kezeli őket. Ebben az esetben a listában szereplő típusnak paraméter nélküli konstruktort kell tartalmaznia.
Adatforrás beállításakor, ha a megadott hivatkozás egynél több listát vagy táblát tartalmaz, a DataMember tulajdonságot olyan sztringre kell beállítania, amely meghatározza a listában a kötést. A tulajdonság beállítása növeli az eseményt DataSourceChanged .
Note
Ha módosítja a DataSource tulajdonság értékét, ezt a felhasználói felület (UI) szálon kell megtennie, hogy a felhasználói felület tükrözze a módosításokat.
A DataSource tulajdonság az osztály alapértelmezett tulajdonsága BindingSource .