简体   繁体   English

C# 实体框架从组合框获取数据

[英]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.我在 Windows 表单上工作,我有用户填充文本框的数据,之后我需要将数据保存到我的上下文(数据库),这是我将数据插入数据库的代码,但我有数据(数字)和一些字符串)用户将从 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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