Refreshing DataGridView After Editing a Record - vb.net

I have a DataGridView bound to a datatable which comes from an SQL Server database.
When the user edits a record my update statement changes the field datetimemodified to reflect the last date and time the record was edited (as stored procedure). The new value for datetimemodified is not brought into my DataGridView.
1) How do I refresh a DataGridView bound to a DataTable? Is there any way to refresh or resync only records that have changed instead of the entire DataTable? (Note: my update statement is working fine. I'm only wondering about refreshing the DataGridView.)
2) Would it be better to change the value of DateTimeModified on the client side so that I can avoid a refresh (assuming that this is the only reason I need to refresh the data)?

fnDraw
function will help you
oTable.fnDraw();
if you using ajax to edit. you can call this function in ajax success

The scenario you described requires to use Domain Objects that implement INotifyPropertyChanged interface. This will allow any change in Domain object to be propagated back in UI. use Domain object with BindingList. Datatables provide flexible but inefficient approach of data-binding. You may find following resource helpful.
How to Implement INotifyPropertyChanged Interface
Datatable vs BindingList

Related

VB.NET How to "Refresh" data DbContext in winform

I have a DataDridview filled with objects queried with my DbContext.
I show some basic informations about my objects in this form.
I would like to query the most recent infos from the BD every time I select a different object in the DataDridview. I also want to be able to modify these objects.
I'm already able to do that. To modify an object, for example, I will do these steps:
-create a new DbContext
-get the object from my Datagridview using .Selectedrows(0).DatabountItem
-with that object's id I will query the (most recent) record in the DB using my new DbContext
-assign the old object (modified) properties to the new ones, one by one
-.SaveChanges on my new Dbcontext.
But there has to a better way, right? :/
If I understand correctly, by doing it this way I end up with a ton of unused Dbcontext and I doubt it's best practices.
But whenever I .Dispose my context, all EF navigation properties are broken and I get exceptions popping all over the place... So the ideal solution seems to me to just, refresh the data in a unique DbContext that I would use for the entire form. But is it even possible?
Maybe the solutions is obvious but I just can't see it.
If I'm unclear, please let me know I'll do my best to reformulate.
Any help would be appreciated, thanks!
** Edit **
In the end, here's the solution that worked best for me:
I do create a new DbContext for every logical operation, then immediatly .Dispose it...
Except for when I show informations about a specific row in my datagridview.
Instead, I create a new Context and leave it open. I will .Dispose only when the datagridview.SelectionChanged event fires.
The reason for not Disposing this context immediatly is: If a user saves his changes, but in the meantime someone else also saved changes on the same record, the (not-synced) context will hit a concurrency issue.. and I can let the user know about it, instead of overriding that row, which would be bad.
If I need these navigations properties from EF elsewhere, I can simply do eager loding by .Include("MyOtherTable") everything I need.( because navigation properties stop working when a context is Disposed)
I would use only one context in this case. You must just pay attention for accesing context from another threads - it can result in transaction errors ("New transaction is not allowed because there are other threads running in the session")
If you use DataGridView with DataSource from EF Context, all you have to do is just reload entities.
I wrote extension method in my context:
public partial class MyContext : testEntities1
{
public void Refresh(IEnumerable entities)
{
var ctx = ((IObjectContextAdapter)this).ObjectContext;
ctx.Refresh(System.Data.Entity.Core.Objects.RefreshMode.StoreWins, entities);
}
}
it is much faster than entry.Reload();
F.E.:
List<MyEntities> items = new List<MyEntities>();
foreach(DataGridViewRow row in dataGridView1.SelectedRows)
{
items.Add((MyEntities)row.DataBoundItem);
}
context.Refresh(items);

jQuery DataTables: How can I explicitly set the table instance name / table ID to use with state saving?

Background:
I'm using DataTables in conjunction with a JS library called "Turbolinks", which basically turns your application into a Single Page Application (SPA) without all the overhead of using a true client-side framework. It is extremely useful for Ruby on Rails application performance.
There's a couple of headaches it introduces though - one is compatibility with DataTables. I've got it working pretty well by basically destroying any DataTable on a Turbolinks navigation, and then re-initializing it on turbolinks page load again. This method works well and seems to be the all-around accepted answer as to the best practice to get DataTables to work with Turbolinks.
Question:
On of the last features / finishing touches I'm trying to add to some of my applications is DataTable state saving. The issue I'm facing is that every time a table is destroyed/re-initialized on a page navigation, the...I'm actually not quite sure what to call it, but it looks like from inspecting the settings object on the stateSaveCallback - it looks like its the sInstance and/or the sTableId:
DataTables_Table_0
Then the localStorage key gets set as:
DataTables_DataTables_Table_0_/current_path: "{data: data}"
where current_path is whatever path/page you're on.
Then when it get re-initialized upon returning to the page, it gets set as DataTables_Table_1, and so on and so forth - so the state never gets correctly loaded.
Is there a way to override that ID (or some way to set the name of it in the stateSaveCallback / stateLoadCallback) so that it doesn't increase the last '0', '1', etc at the end of it? That way when the table is re-initialized, it will pull the saved state from just DataTables_Table/current_path?
The answer is to simply give the table an ID! Then DataTables won't assign it its own ID with the incrementing number and the saveState option just works.
Also, the destroy/re-init actually causes the server to get hit twice in the case of an AJAX table.
The better way to do it is to disable the turbolinks cache for any index pages with datatables. If not, you'll end up doing two requests to the server when only one is needed.

Sencha Touch 2 - "quietly" delete and update records in localstorage

I have a sencha touch 2 web app that is using a localstorage datasource to store a bunch of records.
I am able to perform all the usual crud operations fine, but I want to sync data using a webservice. so periodically, the sencha app will poll the webservice for data changes and then make the necessary changes to the localstorage datasource of my sencha app..
My approach has been to use the following code block to run my sync process every 60 seconds:
var timerID = setInterval(function()
{
MyApp.app.BackgroundProcessingMain();
}, (60000));
Inside "BackgroundProcessingMain()", I have various method calls to sync the various datastores (5)..
I call the webservice and get the data I require back, and then my approach has been to loop through the returned data, filter my store to the id of the current item of the returned data and then either delete it, or update it as necessary.
This works fine.. BUT, if this background process kicks off and I'm viewing a bound list control, my list which is using a filtered version of my datasource, suddenly drops down to only showing a single item, usually the last one in the returned data that needs to be synchronised since it was the last one that my update process filtered the store to operate on.
I thought I could use store.findById, get the record reference and update/delete that way, but if the particular ID is already being filtered out due to the view my bound list requires, the record isn't found in the store and therefore doesn't get updated..
What I'd like to be able to do is get a temporary copy of the store, unfiltered, be able to modify it, and then when my app then queries the localstorage next time a form is shown, it will just get the new updated data..
That is basically what I'm referring to as "quietly" in the title..
Does anyone have a suggestion as to what process I could take to get this update done..??
If you have example code, that would be awesome, but pseudo-code is fine..
Thanks
You can use suspendEvents() and resumeEvents() to temporarily prevent your store from firing events. You can then clear your filters, apply your updates (using store.findById()), and reapply your filters without your list changing.
var store = Ext.getStore('myStore');
store.suspendEvents();
store.clearFilter();
doThings(...);
store.filter(myFilters);
store.resumeEvents();
If you pass true into store.resumeEvents(), the buffered events will be discarded.

GMF Model & Table View

I have been given this task and would appreciate it if someone helped really. I built a Graphical Model through GMF, which is as follows;
As you see one of the nodes in the model has been selected. The task here is to create an eclipse view with a table, which will be automatically updated upon the selection of a so-called "City Node". As you may guess from the model, the table should contain path costs to all of the cities. I will later expand my solution to include a modified Dijkstra Algorithm but right now i am stuck in the creation of a table view.
I tried to build it using a TableViewer but it seems fairly complex since we need to set the input of the table on ContentProvider, but the twist here is that since we need a SelectionListener to obtain the City Coordinates (as it was ordered to calculate path-costs through the distance between two connected cities divided by the max speed, which was indicated on the connecting streets in the graph) and also the currently selected city, the path-costs need to be automatically calculated and displayed in the table upon the receipt of a click-event. This means that we somehow need to update the input which is gonna be handed to ContentProvider on every selection change.
For further information, I get the current selection through selectionChanged method of ISelectionListener interface and inside this method I put the city information in an arraylist. However although i declared this arraylist outside of the method as public, I cannot seem to access it from the outside of the method and thus can not pass it contentProvider. Eventually the input of the table can not be updated. I tried to write this text as simple as possible and I hope you guys can help me. As I can not foresee now what should be done, I would really appreciate it.
You're on the right track!
In your selection listener's ISelectionListener.selectionChanged method you just have to set the new input for the viewer with TableViewer.setInput. Then, the IStructuredContentProvider.inputChanged method gets invoked on the content provider for the viewer. It's here where you can do your stuff with the new input and refresh the viewer with TableViewer.refresh.
You can also use the JFace databinding framework, but I think you should be fine with what I've mentioned above.

Any documentation available for xe:objectData?

It seems, that the objectData control can be used as a performance boost for an xpage application. I understand the basic idea behind, but still have trouble to get it to work properly.
Using a objectData as input for a repeat control avoids the unneccessary refresh of the repeat during a partial refresh that was triggered on another refreshId than the id for the repeat. But due to the cache mechanism in the objectData, the objectData is not refreshed during a partial refresh of the surrounding div for example. Setting the scope to request, objectData is refreshed, but the issue with partial refresh also refreshing datasources outside the refreshId occurs.
A bit weird, I know, but I do not know, how to explain it better.
So is there any documentation or sample on how to use objectData? Found one sample in the JDBC sampleDb, but it did not help.
In the context of the repeat/specific row use case, introduce Partial Execution (execMode="partial" / execId="foo") to complement the Partial Refresh of the row. This will hone component tree execution to the row of interest and avoid redundant processing outside of the specified target area.
In terms of documentation for objectData, the best worked example is indeed within the XPagesJDBC.nsf sample database (JDBC_RowSetDatasource.xsp) - it succinctly demonstrates using this datasource as a delegate to create a specialized DataContainer object from the current "row" variable, and conversely managing the specialized saving of the DataContainer object during the save process. Although this example handles the delegation of SQL processing for the current row, the same approach is applicable under many use cases (eg: one example, the underlying view could be a view of XML documents, where you need to delegate per row handling using the objectData datasource through a custom specialized XML processing object).