This is a migrated thread and some comments may be shown as answers.

Gridview not refreshing on inserts

7 Answers 384 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 17 Jul 2012, 04:06 AM

Hello,

I have a gridview bound to an entity framework collection. To update to database I refresh my collection followed by gridview.Refresh. This works as expected when I change fields of existing entities. 

However, when I add a new entity into the collection, the gridview does not reflect the insertion unless I set its DataSource to null and back to my collection. How can I force a rebind without completely resetting the gridview state like that?

Thanks.

7 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 19 Jul 2012, 01:11 PM
Hi Andrew,

Before I can be of your assistance, I would like to know how you are using the RadGridView. Please share code snippets that demonstrate how you are initializing it.

Thank you for your cooperation.

All the best,
Svett
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Andrew
Top achievements
Rank 1
answered on 25 Jul 2012, 12:12 AM

Hi, 

I have a static instance of an EF context called CoreManager.Context and bind like so:

//create single instance of EF ObjectContext
public static Models.CoreEntities Context = new CoreEntities();

//bind Batches collection to radGridView
this.statusGrid.DataSource = CoreManager.Context.Batches;

//refresh Batches collection
Console.WriteLine(CoreManager.Context.Batches.Count());
CoreManager.Context.Refresh(System.Data.Objects.RefreshMode.StoreWins, CoreManager.Context.Batches);
Console.WriteLine(CoreManager.Context.Batches.Count());
this.statusGrid.Refresh();

If I run two instances of the application and change fields in existing Batches, calling the above refresh displays the changes. However if I add new objects to the collection, they do not show up. The don't even show if I set the radgridview DataSource to null and back to my Batches collection. I must rebind to Batches.ToList() in order to see the changes. 

I have confirmed that a new object is brought into the collection on refresh by printing the number of items in the collection before and after Context.Refresh.

0
Svett
Telerik team
answered on 27 Jul 2012, 10:55 AM
Hi Andrew,

I managed to reproduce the issue. I logged it to our public issue tracking system. It will be addressed in one of the next releases. Meanwhile, you can rebind the RadGridView to refresh it.

I updated your Telerik points.

Regards,
Svett
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Andrew
Top achievements
Rank 1
answered on 27 Jul 2012, 02:02 PM
Thanks for the feedback. Can you please recommend the appropriate method to rebind without calling the likes of toList? I need to use the gridView to insert items into the entity collection so I must bind to it directly. Also, is there a suggested method to persist state like selection and column sizes on a rebind? Thanks.
0
Svett
Telerik team
answered on 30 Jul 2012, 11:41 AM
Hello Andrew,

You can use save and load layout capabilities to persist the column state. Regarding the current row, you can remember the current row index before you rebind the RadGridView. You can use the following code snippet as sample:
using (MemoryStream ms = new MemoryStream())
{
    this.radGridView1.SaveLayout(ms);
    int currentRowIndex = this.radGridView1.CurrentRow.Index;
    this.radGridView1.DataSource = yourDataSource;
    this.radGridView1.CurrentRow = this.radGridView1.ChildRows[currentRowIndex];
    ms.Seek(0, SeekOrigin.Begin);
    this.radGridView1.LoadLayout(ms);
}

I hope this helps.
 
Kind regards,
Svett
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Andrew
Top achievements
Rank 1
answered on 11 Oct 2012, 03:44 AM

Hi again, I'd like to clarify how I can rebind the gridview to an EF collection without calling .toList(). 

Works:

gridView.DataSource = someCollection

Doesn't refresh:

context.refresh()

gridView.DataSource = someCollection

Current solution:

context.refresh()

gridView.DataSource = someCollection.toList()

The problem is that List has no change tracking so inserts/deletes in the gridview aren't pushed back to EF. Is there some way to completely reset the state of the gridView so I can bind straight to someCollection again?

0
Svett
Telerik team
answered on 15 Oct 2012, 12:48 PM
Hi Andrew,

You could call the Refresh method of the MasterTemplate property to refresh the RadGridView instance. You can use the following code snippet:
this.radGridView1.MasterTemplate.Refresh();
Kind regards,
Svett
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
Tags
GridView
Asked by
Andrew
Top achievements
Rank 1
Answers by
Svett
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or