简体   繁体   English

DataGridView 是可编辑的,但不会使用实体框架将更改发送回数据库

[英]DataGridView is editable but does not send changes back to database using Entity Framework

I've bound my DataGridView to my ComboBox so that whatever value is selected in the ComboBox , the corresponding valued for SID and Mark will appear in the DataGridView .我已将我的DataGridView绑定到我的ComboBox以便无论在ComboBox选择什么值,SID 和 Mark 的相应值都将出现在DataGridView The DataGridView is editable when I do this but the data is not saved in the database when it is input.当我这样做时, DataGridView是可编辑的,但输入时数据不会保存在数据库中。 Is there a way to update it?有没有办法更新它? If there's another method, I have to first warn that I only need SID and Mark in the DataGridView , if I try to bind the whole "Student_Course" table to the DataGridView I get other columns I don't need.如果有另一种方法,我必须首先警告我只需要DataGridView中的 SID 和 Mark,如果我尝试将整个“Student_Course”表绑定到DataGridView我会得到我不需要的其他列。

private void cboeCID_SelectedIndexChanged_1(object sender, EventArgs e)
{
    var CID = Convert.ToInt32(cboeCID.Text);
    using (var db = new Entities2())
    {
        var course = from c in db.Student_Course
                     where c.CID == CID
                     select new Class1
                     {
                         SID = c.SID,
                         Mark = c.Mark
                     };
        editDataGridView.DataSource = course.ToList();
        Validate();
        editDataGridView.EndEdit();
        editDataGridView.Update();
    }
}

class Class1
{
    public int SID { get; set; }
    public int Mark { get; set; }

}

There are some important issues in above code:上面的代码有一些重要的问题:

  1. You shaped the result of query to a custom Class1 which is not your entity type.您将查询结果整形为自定义Class1 ,这不是您的实体类型。

  2. You used a DbContext in using statement which means db is disposed after the using statement and will not track changes.您在using语句中使用了DbContext ,这意味着dbusing语句之后被DbContext并且不会跟踪更改。

  3. You called SaveChanges on another instance of your DbContext which is not aware of changes, so nothing happens.您在DbContext另一个不知道更改的实例上调用了SaveChanges ,因此没有任何反应。

To solve above issues consider these tips:要解决上述问题,请考虑以下提示:

  1. Create db as a field of Form and instantiate it in Load event of Form and use it for both loading and saving data.创建db作为Form一个字段,并在Form Load事件中实例化它,并将其用于加载和保存数据。
  2. You can load data entity this way:您可以通过以下方式加载数据实体:

     db = new Entities2(); db.Student_Course.Where(x => c.CID== CID).ToList(); editDataGridView.DataSource = db.Student_Course.Local;
  3. You can save data this way:您可以通过以下方式保存数据:

     editDataGridView.EndEdit(); db.SaveChanges();
  4. If you need to use a view model different than your entity for edit, when saving changes you should first load original entities from database using another instance of your context, then for each entity set the value of changed field and then call SaveChanges method.如果您需要使用与实体不同的视图模型进行编辑,则在保存更改时,您应该首先使用上下文的另一个实例从数据库加载原始实体,然后为每个实体设置已更改字段的值,然后调用SaveChanges方法。

For more information take a look at these resources:有关更多信息,请查看以下资源:

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

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