Better way of refreshing DataGridView.DataSource
Currently I have a DataGridView
in my app which I fill from my Linq-to-SQL classes like so ...
/// <summary>
/// Delegate to update datagrid, fixes threading issue
/// </summary>
private void updateInfo()
{
// Linq to datagridview;开发者_运维百科 display all
var query = from n in dbh.Items select n;
itemDataGridView.DataSource = query.ToList();
}
So the problem with this is every time I add new information and run this refresh, the focus of the table changes, meaning if I'm on one row it will switch me to an another one. And there is an event tied to the change of row so this causes this event to run while the list keeps refreshing.
I don't but I remember before switching to Linq-to-SQL bounded DataGridView
there was a TableAdapter
formed, and instead of altering data in the DB you would just insert using this TableAdapter
which would automatically refresh the DataGridView
in the proper way.
So I'm wondering if there is some other way I should be doing this to each that smooth refresh?
This doesn't sound like a L2S issue to me. This sounds more like an issue with whatever grid you are using. All L2S does is to supply your grid with data. It makes sense to me that the current row of the grid would change after a refresh. Is it possible for you to disable the grid, set the current row to be what you want, and then enable the grid? I've done this in the past.
Randy
It appears you are trying to modify the data outside of your application and then have your DataGridView display those changes.
I am not aware of a way to automatically poll the database and then have the DataGridView only update that row to show a smoother refresh. For refreshing, I have always re-bound the DataGridView to the DataSource which contains the TableAdapter Select,Update, and DeleteCommand (SQL but similar). I do not think this is a LINQ problem.
Regardless of how you are updating your underlying table, there is going to be a refresh of some kind unless you are editing just one field and then post the updates later.
I assume you are using WinForms and then there is no alternative. If you are using WPF however it is possible, using the binding techniques that exist in WPF. I do not know the details however.
As others have already pointed out, your problem is not related to Linq2Sql per se, it's in the way DataGridView
handles object data sources.
One way to work around the lost selected row is to sort the collection you are binding against and programmatically select the previously selected row after refresh.
精彩评论