我有一个使用实体框架的简单项目,我有一个DataGridView,Form并将其AllowUserToAddRow属性设置为true,但我仍然无法向其中添加新行。
这是我的代码:
DBEntities context = new DBEntities();
private void Form1_Load(object sender, EventArgs e)
{
var q = (from i in context.myTable
select i).ToList();
DataGridView.DataSource = q;
}
private void btnSave_Click(object sender, EventArgs e)
{
context.SaveChanges();
MessageBox.Show("saved successfully");
}
Run Code Online (Sandbox Code Playgroud)
如果我使用BindingSource控件,它允许我插入行,DataGridView但是在context.SaveChanges()我在数据库文件中不调用任何插入之后,使用这种方法。所以我想也许与这个问题有关,DataGridView属性true AllowUserToAddRow不允许我在 中插入行DataGridView。