DataGridView AllowUserToAddRow属性不起作用 [英] DataGridView AllowUserToAddRow property doesn't work

查看:426
本文介绍了DataGridView AllowUserToAddRow属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题。

第一个问题是我有一个简单的实体框架项目,我有一个 DataGridView 在我的表单中,我将 AllowUserToAddRow 属性设置为 true 但仍然无法添加新行。

First problem is I have a simple project with Entity Framework, I have a DataGridView in my Form and I set its AllowUserToAddRow property to true but still I can not add new rows into it.

这里是一个屏幕截图,看看我的 DataGridView 在运行时看起来如何。

Here is a screenshot to see how my DataGridView looks like in run-time.

这里是我的代码:

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 control,它允许我在 DataGridView 中插入行,但在调用 context.SaveChanges()在我的数据库文件中没有插入。所以我认为也许它相对于这个问题, DataGridView true AllowUserToAddRow 属性不允许我在 DataGridView 中插入行。

Second problem is If I use a BindingSource control, it allow me to insert rows in DataGridView but with this approach after I call context.SaveChanges() nothing insert in my database file. So I thought maybe its relative to this problem that DataGridView with true AllowUserToAddRow property doesn't let me to insert row in DataGridView.

推荐答案

您的问题是您调用 .ToList()并实现您的查询 - 这似乎打破了完整的数据绑定。

Your problem is that you call .ToList() and materialize your query - this appears to break the full databinding.

应该可以简单地:

DBEntities context = new DBEntities();
private void Form1_Load(object sender, EventArgs e)
{
    var q = (from i in context.myTable
             select i);
    DataGridView.DataSource = q;
}

我尝试过这个,它可以正常运行,允许新的行(你需要在你的表中有一个主键,但你应该有这个)。

I tried this and it works fine for allowing new rows (you do need to have a primary key in your table but you should have that anyway).

注意: Entity Framework 4.1中已故意破坏此行为 - Webforms数据绑定与EF Code-First Linq查询错误

Do Note: this behaviour has been intentionally broken in Entity Framework 4.1 - Webforms data binding with EF Code-First Linq query error

我说应该

I say should in my answer because I'm actually a little surprised it is this easy. I recall it not working so nicely in earlier versions of Entity Framework and I haven't used 4.0 very much.

如果上面的解决方案不起作用,你可能必须在保存之前,请自行修改此方法并自行添加新对象:

If the solution above doesn't work you may have to do this the hard way and add new objects yourself before saving:

首先介绍一个绑定源,当你保存时,做一些例子(在客户的虚构实体中) ):

First introduce a binding source and when you save do something like (with an imaginary entity of Customer in the example):

foreach (Customer customer in bs.List)
{         
    // In my db customerId was an identity column set as primary key
    if (customer.CustomerId == 0)
        context.Customers.AddObject(customer);
}
context.SaveChanges();

这篇关于DataGridView AllowUserToAddRow属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆