views:

1063

answers:

3

Hi,

I have a GridView who's DataSource is set to a DataTable. The DataTable is updated by some backend logic every few seconds, at which point a delegate is called to refresh GridView.

Currently I am simply resetting the DataSource, but that causes a problem - it interrupts any ongoing edits in the grid view and makes the selection 'jump' to the top-left cell.

The update logic basically creates a new (identical with regard to columns and rows) DataTable.

Is there any standard way to do it without any drawbacks? Is my only option updating the current DataSource row by row, inserting values programmatically?

Thanks!

+1  A: 

You should use a BindingList or some data source that supports change notification.

leppie
A: 

Do your updates happen on a background thread? I don't know if it will work in your scenario, but you could try this threaded binding list; see the example to see the worker merrily editing the grid.

Marc Gravell
+1  A: 

I'm confused by many things in this question. If you're using a GridView, and not a DataGridView, then you're either using ASP.NET, WPF, or .NET 1.1. Which is it?

Next: you're creating a new DataTable entirely? Well of course the control's going to get reset when you reset the DataSource. It doesn't know that the schema of your new DataTable is the same as the one it's replacing. It's got to go through the columns and re-establish the bindings.

Also, of course it's losing the current row. The current row belongs to the old DataTable, not the new one.

If you want a bound control to retain its state when you update the underlying data source, update the underlying data source, don't replace it with a new one.

Robert Rossney
I'm indeed using a DataGridView.The new DataTable has the same schema, and is simply generated from some database and modified by some process.Of course I know _why_ the row is lost. My question is how should I do it so I won't have to replace the DataTable...
Oh, and I have no idea how to update an existing DataTable using a new one (I'm reluctant to scan it field by field...)
Take a look at the DataTable.Merge method. That may well do what you want.
Robert Rossney