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
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.
Svett
the Telerik team
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.
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
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
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?
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();
Svett
the Telerik team