locked
How to Delete rows in DB found in a datagrid from code behind using EF RRS feed

  • General discussion

  • Found a solution to deleting rows in a datagrid from code behind, when EF binding is used.  Perhaps this will help someone someday!

    This Fails:

    DataGrid.Items.Clear();  //fails because Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

     This works:

    using (Models.Entities1 ae1 = new Models.Entities1())
    {
        foreach (var thing in DataGrid.Items.OfType<entity>())
        {
            ae1.Attach(thing);
            ae1.DeleteObject(thing);
            ae1.SaveChanges();
        }
    }

           


    JP Cowboy Coders Unite!


    • Edited by Mr. Javaman II Tuesday, October 23, 2012 6:59 PM
    • Changed type Allen_MSDN Wednesday, October 24, 2012 1:47 AM
    Tuesday, October 23, 2012 6:58 PM

All replies

  • Hi Mr. Javaman,

    Thanks for your sharing.

    Have a nice day.


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 24, 2012 1:46 AM