binding winforms to database by entity framework 6

.net c# entity-framework entity-framework-6 winforms

Question

The case is straightforward:

  1. MyDbContext is an EF6 DbContext.
  2. I have a list box on my form.
  3. The following code will tie data from a database to a listbox:

    dbContext = new MyDBContext();
    listBox1.DataSource = dbContext.Experiments.Select(a => a.Name + "," + a.ModificationDate.Second).ToList();
    
  4. I have the following button to add a row to a database table:

     dbContext.Experiments.Add(new Models.Experiment()
        {
            CreationDate = DateTime.Now,
            ModificationDate = DateTime.Now,
            Description = "Test" + DateTime.Now.Second,
            Name = "test" + DateTime.Now.Second
        });
        dbContext.SaveChanges();
    

I now want the locations in my listbox to automatically refresh. What is the most effective method to do that? I am aware that I can rebuild the code from point 3, but I would prefer to have it all be automatically updated and two-way.

1
1
12/31/2014 1:12:15 PM

Popular Answer

ZZZ_tmp
1
12/31/2014 2:17:19 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow