【问题标题】:C# entity framework get data from comboboxC#实体框架从组合框获取数据
【发布时间】:2017-05-23 00:29:45
【问题描述】:
            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();
}

我在 Windows 表单上工作,我有用户填写文本框的数据,之后我需要将数据保存到我的上下文(数据库),这是我将数据插入数据库的代码,但我有用户将从 ComboBox 中选择的数据(数字和一些字符串)。我需要获取这些数据并将其保存到对象列表中,这是代码:

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

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

上下文:

  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; } 
    }

我需要从组合框中获取此数据到成员列表并将其保存到对象(成员)列表中

【问题讨论】:

  • 您使用的 GUI 技术是什么 - WinForms、WPF、WebForms...?该 ComboBox 中包含什么 - 它有一些 DataSource 还是直接填充?
  • Windows 窗体和组合框有数据(数字和字符串)
  • 最初问题中的那个should have been mentioned。那数据是什么(一些特殊的类,或Groups)?添加您的具体填写方式。
  • 我在我的问题中指定了这一点

标签: c# entity


【解决方案1】:

这取决于组合框中的内容。

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
}

【讨论】:

  • 我更新了一些信息,对不起,我是新来的:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多