I am running into an issue with refreshing data on a page after doing an update through WCF RIA Services.
I have a ComboBox and a Button on a page. The user chooses an item from the Co开发者_如何学JAVAmboBox, and then clicks the Button. This does a soft delete of the item in the database(setting "Active" = false). However, I would like for it to be removed from the ComboBox after the updated has completed. This is where my problem is.
InventorySystemDomainContext context = new InventorySystemDomainContext();
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
Cigarette c = cboCigarette.SelectedItem as Cigarette;
c.Active = false;
SubmitOperation so = context.SubmitChanges();
so.Completed += delegate (object s, EventArgs es)
{
LoadComboBox();
}
}
private void LoadComboBox()
{
cboCigarettes.DataSource = null;
cboCigarettes.DataSource = context.Cigarettes;
context.Load(context.GetCigarettesQuery());
}
When the Delete button is clicked, all the code runs. However, the deleted item is still in the ComboBox(even though the record has been updated in the database). If I refresh the page, the item is gone from the ComboBox.
Any ideas?
PS: I wrote this code from memory since I don't have the code with me. So I may have forgotten a line, but I think I got all the relevant lines.
Have you tried setting the ComboBox.ItemsSource to the Entities collection return from the Load method? EntitySets (context.Cigarettes) are aggregating (they contain data from multiple loads) while LoadOperation.Entities is not (it only contains data from the load it represents).
The problem with the accepted solution is that the collection is now disconnected, which messes with the built in elegance of adding and removing bound items.
Perhaps a better solution would be to detach the entity in question after the soft delete
InventorySystemDomainContext context = new InventorySystemDomainContext();
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
Cigarette c = cboCigarette.SelectedItem as Cigarette;
c.Active = false;
SubmitOperation so = context.SubmitChanges(OnCigaretteSaved, null);
}
private void OnCigaretteSaved(SubmitOperation so)
{
context.Cigarettes.Detach(context.Cigarettes.Where(item => item.Active == false).First());
}
private void LoadComboBox()
{
cboCigarettes.DataSource = null;
cboCigarettes.DataSource = context.Cigarettes;
context.Load(context.GetCigarettesQuery());
}
精彩评论