Bindingcontext
-
13 august 2005 08:19
hello everyone,
can someone pls tell me the difference between me.bindingcontext(datasource.datatable).addnew and me.bindingcontext(datasource,"datatable").addnew becoz only the latter one works and the other gives errors.(Only the one with comma and qoutes works whilst the fullstop one doesnt)
Thank U
Toate mesajele
-
14 august 2005 16:02
Windows Forms data binding does not bind directly to ADO.NET DataTables - rather it binds to ADO.NET DataViews (provided automatically by ADO.NET). A DataView provides services such as searching, sorting, filtered and "AddNew". You can have a single table with multiple views - for example, the same data table can have one view that sorts by "LastName" and another that is sorted by "FirstName".
When you bind to a DataGrid control (or equivalent), the Windows Forms runtime will get a view from ADO.NET and the DataGrid will bind to that view. Views are shared (and cached) across controls such that multiple controls can share the same view. The BindingContext is used to share/cache views between controls. The BindingContext is keyed off the combination of "DataSource" and "DataMember". Different combinations of these will generate different DataViews (thats just the way it works). In other words, binding a DataGrid as DataSource = DataSet and DataMember = "Table" will use a different DataView than just setting the DataSource to DataSet.Table.
What's happening when you call "AddNew" is you are calling this on a specific DataView. The "AddNew" that doesn't work is being called on a different view than the one the DataGrid is using. The reason it is different is because you are using a different DataSource/DataMember combination than you use when binding to the DataGrid. Note that "AddNew" doesn't auto-commit the "AddNew" row, so when you call "AddNew", the new item only exists in the DataView. In order to commit it and make it appear in all DataViews, you need to call Me.BindingContext[...].EndCurrentEdit().
Joe Stegman
The Windows Forms Team
Microsoft Corp.This posting is provided "AS IS" with no warranties, and confers no rights.
-
14 august 2005 18:281) If you bind like:
dg.DataSource = dataSource
dg.DataMember = "Table1"
then to add new you will HAVE to do:
Me.BindingContext(dataSource, "Table1").AddNew() 'and then EndCurrentEdit
2) If you bind like:
dg.DataSource = dataTable 'This is Table1
then to add new you will HAVE to do:
Me.BindingContext(dataTable).AddNew() 'and then EndCurrentEdit
Thanks,
-Dinesh Chandnani