【发布时间】:2012-08-03 16:26:21
【问题描述】:
我有一个使用 Entity Framework 的简单项目,我的 Form 中有一个 DataGridView,我将其 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");
}
如果我使用BindingSource 控件,它允许我在DataGridView 中插入行,但是在我调用context.SaveChanges() 之后,我的数据库文件中没有插入任何内容。所以我认为可能与这个问题有关的是DataGridView 和true AllowUserToAddRow 属性不允许我在DataGridView 中插入行。
【问题讨论】:
标签: c# linq entity-framework datagridview bindingsource