Can't get DataGridView to refresh over Linq to SQL (WinForm)
Posted
by GringoFrenzy
on Stack Overflow
See other posts from Stack Overflow
or by GringoFrenzy
Published on 2009-12-14T15:54:33Z
Indexed on
2010/06/10
17:12 UTC
Read the original article
Hit count: 239
Very strange situation here: I'm using L2S to populate a DataGridView.
Code follows:
private void RefreshUserGrid()
{
var UserQuery = from userRecord in this.DataContext.tblUsers
orderby userRecord.DisplayName
select userRecord;
UsersGridView.DataSource = UserQuery;
//I have also tried
//this.UserBindingSource.DataSource = UserQuery;
//UsersGridView.Datasource = UserBindingSource;
UsersGridView.Columns[0].Visible = false;
}
Whenever I use L2S to Add/Delete records from the database, the GridView refreshes perfectly well.
However, if someone is editing the grid and makes a mistake, I want them to be able to hit a refresh button and have their mistakes erased by reloading from the datasource.
For the life of me, I can't get it to work.
The code I am currently using on my refresh button is this:
private void button1_Click(object sender, EventArgs e)
{
this.DataContext.Refresh(RefreshMode.OverwriteCurrentValues);
RefreshUserGrid();
}
But the damn GridView remains unaffected. All that happens is the selected row becomes unselected.
I have tried .Refresh()
, .Invalidate()
, I've tried changing the DataSource to NULL and back again (all suggestions from similar posts here)....none of it works. The only time the Grid refreshes is if I restart the app.
I must be missing something fundamental, but I'm totally stumped and so are my colleagues.
Any ideas?
Thanks!
© Stack Overflow or respective owner