c# - Can I bind DbSet and BindingList together to display database contents in the ListBox? -


i use winforms , entity framework 6. have an:

  public class applicationdbcontext : dbcontext {         public dbset<person> people{ get; set; }   } 

every person has properties: id,name,lastname,age.

in form display people in listbox , keep contents of listbox synchronized database.

how bind bindinglist bindinglist applicationdbcontext context, or other way around?

comment: sscce.

you can use tobindinglist() extension method bindinglist<person> need datasource in listbox:

public partial class yourform : form {     private yourcontext context=new yourcontext();      public bindinglist<person> bindinglist { get; set; }       private void yourform_load(object sender, eventargs e)     {         context.people.load();         this.listbox1.datasource= bindinglist= context.people.local.tobindinglist();         this.listbox1.displaymember = "name";     }      //button save new changes     private void savechangesbutton_click(object sender, eventargs e)     {         context.savechanges();     }      //disposing context before close form     private void yourform_formclosing(object sender, formclosingeventargs e)      {          context.dispose();      }  } 

when object added or deleted dbset added or removed bindinglist. adding or removing bindinglist perform corresponding add/remove on dbset.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -