LINQ2SQL - Binding result to a grid - want changes to be reflected without re-binding?
I have a开发者_开发知识库 grid (DevExpress XtraGrid, if that matters) which is bound to a LINQ to SQL Entity property.
gridItems.DataSource = purchaseOrder.PendingItemsGrouped;
Well, the grid is being displayed properly,and I can see the purchase items that are pending. The problem arises when purchaseOrder.PendingItemsGrouped gets changed ... once that happens, the grid does not reflect the changes.
The exact procedure is as following: The user selects a row from the grid, inserts a serial number on a specific textbox, and then hits enter effectively receiving this item from the purchase order, and inserting it into stock.
inventoryWorker.AddItemToStock( userSelectedItem, serialNumber );
The item gets properly inserted to the inventory, but the grid still shows the item as if it is still awaiting it to be received.
How do I solve this problem? Do I really need to re-bind the grid so the changes can be reflected?
I even tried instead of:
gridItems.DataSource = ...;
This:
gridItems.DataBindings.Add( new Binding( "DataSource", purchase, "PendingItemsGrouped" ) );
But couldn't solve the problem. Thank you very much for your time,
Isaac.
OBS: Re-Binding the Grid works, but my question is ... is that even the proper way of doing things? I feel like I'm miles off the right track.
Calling databind is actually the right approach when you think about how databinding in webforms works. In all examples when databinding to objects, calls to databind happen whenever the collection is modified.
The reason it feels wrong is because its a lot cleaner to use a DataSourceControl
, such as a LinqDataSourceControl
or ObjectDataSourceControl
, where all that stuff is handled for you.
Two things that might help you along this path is when using a LinqDataSourceControl
, you might need to override the various -ing methods (Selecting, Inserting, Deleting) etc, in order to add additional filtering and logic.
The other thing that springs to mind is http://multitierlinqtosql.codeplex.com/. Especially the section on Custom ObjectDataSource.
I have not tried it myself but Bindable LINQ allows to achieve it.
精彩评论