none
strange Binding behavior from Entity Framework DataContext to BindingSource RRS feed

  • Question

  • I've bind a EF ObjectContext whith BindingSource and a Grid bind to BindingSource under winforms.

    SomeBindingSource.datasource =  ObjectContex.SomeObjectSet;

    SomeDataGridView.datasource = SomeBindingSource;

    When I insert object into ObjectContext without SaveChanges, it happend nothing.(yes I know the reason, usually I put entities into SomeBindingSource and everything goes fine)

    But when I delete something from ObjectContext, the Grid changed. Entities deleted from Context automaticly removed from Grid, this result make me really confused.

    if BindingSource takes no message from insert Event of ObjectContext, why they are response to delete Event?

    • Moved by Helen Zhou Thursday, August 18, 2011 6:27 AM (From:Windows Forms Data Controls and Databinding)
    Wednesday, August 17, 2011 9:45 AM

Answers

  • Hello,

    I suggest you can refresh context after you inserted into database. I made a test on my side. Please check the following code snippets. Hope this helps.

      public partial class Form1 : Form
      {
        //create new instance of your context
        XueyunEntities1 ctx = new XueyunEntities1();
        public Form1()
        {
          InitializeComponent();
          //initialize datasource
          dataGridView1.DataSource = ctx.Parents.ToList();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {      
          //add record
          Parent parent = new Parent
          {
            Price = 4444,
            startDate = 5555
          };
          ctx.AddToParents(parent);
          ctx.SaveChanges();
          //re-bind context
          this.dataGridView1.DataSource = ctx.Parents;
        }
    
      }
    
    
    Thanks,
    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Larcolais Gong Thursday, August 25, 2011 9:50 AM
    Friday, August 19, 2011 12:13 PM

All replies

  • Hello asker1982,

    This question is more related to  ADO.NET Entity Framework and LINQ to Entities forum, I'm moving it there to make it get better help. Thank you for your understanding.

    Regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 18, 2011 6:27 AM
  • Hello,

    I suggest you can refresh context after you inserted into database. I made a test on my side. Please check the following code snippets. Hope this helps.

      public partial class Form1 : Form
      {
        //create new instance of your context
        XueyunEntities1 ctx = new XueyunEntities1();
        public Form1()
        {
          InitializeComponent();
          //initialize datasource
          dataGridView1.DataSource = ctx.Parents.ToList();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {      
          //add record
          Parent parent = new Parent
          {
            Price = 4444,
            startDate = 5555
          };
          ctx.AddToParents(parent);
          ctx.SaveChanges();
          //re-bind context
          this.dataGridView1.DataSource = ctx.Parents;
        }
    
      }
    
    
    Thanks,
    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Larcolais Gong Thursday, August 25, 2011 9:50 AM
    Friday, August 19, 2011 12:13 PM