简体   繁体   中英

C# entity framework get data from combobox

            private void Submit_Click(object sender, EventArgs e)
{
                ScoutContext db = new ScoutContext();
                ScoutData cust = new ScoutData();
                cust.FName = textBox1.Text;
                cust.LName = textBox2.Text;
                cust.FName = textBox3.Text;
                cust.FaWork = textBox4.Text;
                cust.MoName = textBox5.Text;
                cust.MaWork = textBox6.Text;
                cust.PlaceOfBirth = textBox7.Text;
                cust.City = textBox8.Text;
                cust.School = textBox9.Text;
                cust.FaceBook = textBox10.Text;
                cust.Phone = textBox11.Text;
                cust.MPhone = textBox12.Text;
                cust.IDNumber = textBox13.Text;
                cust.NOfQaid = textBox14.Text;
                cust.GroupID = ?????????????????


                db.SaveChanges();
}

i work on Windows form, i have this data that the user fill the textbox, after that i need to save the data to my context ( database ), this is my code to insert data to my data base , but i have data ( numbers and some string ) the user will chose from ComboBox. i need to get this data and save it to a list of object , this is the code :

 public class Groups
    {
        [Key]
        public string GroupsID { set; get; }

        public string NameOfGroup { set; get; }
        ***public virtual List<ScoutData> Members { set; get; }***
    }

The context:

  public class ScoutContext : DbContext
    {
        public ScoutContext()
            : base("Scout")
        {
        //    if (!Database.Exists("ScoutData"))
        //        Database.SetInitializer(new DropCreateDatabaseAlways<ScoutContext>());
        }
        public DbSet<ScoutData> ScoutDatas { set; get; }
        public DbSet<Groups> GroupesScout { set; get; } 
    }

i need to get this data from the combobox to Members list and save it to a list of object (Members)

It depends on what you have in the combobox.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var combo = sender as ComboBox;

    // If combobox has ScoutData then do this
    var item = combo.SelectedItem as ScoutData;

    // If combobox has something else then do this
    var item2 = combo.SelectedItem as SomeThingElse;
    var newScout = new ScoutData { FName = item2.FName /*, etc, etc */  };

    // Then add it to your list
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM