Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > databound controls not updating - confusion between Linq datacontext/table objects and ado.net dataset/table objects
 

databound controls not updating - confusion between Linq datacontext/table objects and ado.net dataset/table objects

I have a datacentric windows forms application performing CRUD functions to a database with 3 tables: rcTable (real estate); scTable (different real estate); and imgTable (pictures of both types of real estate). Data entry forms were designed using the windows forms designer and VB code-behind has been extensively modified with Linq-enabled VB code for data validation and manipulation.

The problem I am having is that my datagridview controls throughout the application do not reliably update to reflect records which have been manually inserted and deleted via the Linq datacontext. If I exit and re-run the application, the changes show up in the datagridview controls.

I suspect that I have some inconsistency in the databinding of my controls between the Linq and ado.net objects. In this regard I have several questions regarding how databinding should happen, how insert/delete and methods should be wired, and how/whether the ado.net and Linq dataset objects should coexist.

First of all, the forms designer automatically inserts code like the following in my forms where I have defined a dataset connection:

'TODO: This line of code loads data into the 'REdataDataSet.PropImages' table. You can move, or remove it, as needed.
'Me.PropImagesTableAdapter.Fill(Me.REdataDataSet.PropImages)

Can this code be deleted?

I have also used the forms designer to put a binding navigator on my forms. Should the binding navigator's add/delete/change hooks be wired to the linq objects or the ado.net table adapter? Or does it matter?

Because I am trying to use Linq to SQL for all of my data manipulation, I also have something like the following in the declarations region of my forms modules:

Private db As New DataContext(myConfig.dbActive)
Private imgTbl As Table(Of PropImage) = db.GetTable(Of PropImage)()
Private rcTbl As Table(Of RentComp) = db.GetTable(Of RentComp)()
Private scTbl As Table(Of SaleComp) = db.GetTable(Of SaleComp)()

Clearly, this creates Linq table objects for use within the form module, but does it also create duplicate and possibly inconsistent Linq objects in other form modules? For data inserts, deletes, and changes I assume I should be using InsertOnSubmit/DeleteOnSubmit on the Linq table objects, SubmitChanges to the in-memory datacontext, but how/when are the disk-images of the tables and database updated? Is this automatic or do I need to do anything else?

Instead of the inital assignment statements in the declarations section, should I have a refresh method that I can call after any data changes? Maybe something like the following:

Private Sub dbReBind()
db.Refresh(RefreshMode.OverwriteCurrentValues)
imgTbl = db.GetTable(Of PropImage)()
rcTbl = db.GetTable(Of RentComp)()
scTbl = db.GetTable(Of SaleComp)()
End Sub 're-bind datacontext elements

What about controls which may be mistakenly databound to ado.net dataset objects? Are these automatically updated when the database is updated? Do I need to do anything to implement iNotifyPropertyChanged? If so, how or where does this code go? I would like for my databound controls to be automatically updated in the simplest manner.

Any specific help or references to understandable resources would be appreciated.

Thanks,

-BGood

BGood  Tuesday, September 29, 2009 5:52 PM

Hi BGood,

We need to make some concepts clear about data binding module. When we use data binding to implement CRUD functions, there are three tier in the software architecture: UI(front, show data), binding source(middle, store data in memory), database(back, persist the data). We would discuss about the relations between these three tiers as follows:

1. Relation between UI and binding source. The data binding module handles the synchronization between the binding source and the UI. For example, if we bind a binding source to a DataGridView, what we can see in the DataGridView would be consistent with the data underlying(binding source). When we change the data in binding source, the values in DataGridView would be changed; when we edit values in DataGridView, the data in binding source would also be changed.

2. Relation between binding source and database.
1) The data is not automatically synchronized
.Net provide so many classes to simplify the data fetching and updating from/to database, but there is no automatic method to do this like the data synchronization between UI and binding source. It is not because Microsoft cannot do that, but to provide the flexibility for the developers to do more custom designs and have them be able to control the efficiency of their program. For example, if the database updating is implemented automatically, when we need to modify 100 cells in DataGridView, the database would be updated 100 times. This means there would be 100 database connections created and so many sql scripts executed.
2) How would we fetch and update data. To fetch data from database, we need to use DataAdapter or linq to fill the binding source as follows:
Me.PropImagesTableAdapter.Fill(Me.REdataDataSet.PropImages)

To update data to database, we need to call the EndEdit method of the binding source to update the data to DataSet(DataTable) at first, then we can call the update method of the DataAdapter to do the updating.
All the methods above are about ADO.Net. It is similar if we want to do this with linq, You can follow the samples below:
http://www.codeproject.com/KB/linq/CRUDLINQ.aspx
http://www.codeproject.com/KB/architecture/EnterpriseApplicationArch.aspx

Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, October 01, 2009 7:48 AM

Hi BGood,

We need to make some concepts clear about data binding module. When we use data binding to implement CRUD functions, there are three tier in the software architecture: UI(front, show data), binding source(middle, store data in memory), database(back, persist the data). We would discuss about the relations between these three tiers as follows:

1. Relation between UI and binding source. The data binding module handles the synchronization between the binding source and the UI. For example, if we bind a binding source to a DataGridView, what we can see in the DataGridView would be consistent with the data underlying(binding source). When we change the data in binding source, the values in DataGridView would be changed; when we edit values in DataGridView, the data in binding source would also be changed.

2. Relation between binding source and database.
1) The data is not automatically synchronized
.Net provide so many classes to simplify the data fetching and updating from/to database, but there is no automatic method to do this like the data synchronization between UI and binding source. It is not because Microsoft cannot do that, but to provide the flexibility for the developers to do more custom designs and have them be able to control the efficiency of their program. For example, if the database updating is implemented automatically, when we need to modify 100 cells in DataGridView, the database would be updated 100 times. This means there would be 100 database connections created and so many sql scripts executed.
2) How would we fetch and update data. To fetch data from database, we need to use DataAdapter or linq to fill the binding source as follows:
Me.PropImagesTableAdapter.Fill(Me.REdataDataSet.PropImages)

To update data to database, we need to call the EndEdit method of the binding source to update the data to DataSet(DataTable) at first, then we can call the update method of the DataAdapter to do the updating.
All the methods above are about ADO.Net. It is similar if we want to do this with linq, You can follow the samples below:
http://www.codeproject.com/KB/linq/CRUDLINQ.aspx
http://www.codeproject.com/KB/architecture/EnterpriseApplicationArch.aspx

Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, October 01, 2009 7:48 AM

You can use google to search for other answers

Custom Search

More Threads

• Gridview Delete Update
• Adding Column Data To New DataGridView Row
• Insert/Delete in Datagrid
• Adding a DataGridViewComboBox to DataGridView, using the value selected for calculations in Custom Column
• listBox takes a long time to update
• Custom DataGridViewTextBoxColumn doesn't cause DataGridView.CellFormatting event to fire
• Databinding question
• Error using Data Source Configuration Wizard in VS 2005
• asp.net c# .DataSource, .DataMember, .DataTextField, .DataValueField, .DataBind
• how to populate a DGV cell with text from a textbox (C#)