添加行编程到正在数据绑定一个DataGridView? [英] Adding rows programatically to a DataGridView that is data bound?

查看:110
本文介绍了添加行编程到正在数据绑定一个DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我似乎无法能够解决它。我还以为有人在这里将能够提供帮助。

I have a problem and I can't seem to be able to solve it. I thought someone here will be able to help out.

我有了客户的一个DataGridView的形式。现在,我想给多个客户添加到该DataGridView中实际上不将它们添加到数据库中。这是因为客户端必须能够创建客户端列表,当多数民众赞成做一次全部添加

I have a form that has a DataGridView of customers. Now, I want to add several customers to this DataGridView without actually adding them to the database. This is because the client must be able to create a list of clients and when thats done add them all at once.

我已经试过这样:

string[] array = {"Microsoft", "Redmond", "Something"}
dataGridView.Rows.Add (array);

现在这个不能做,因为我得到一个异常说在东西线的你不能编程行添加到一个DataGridView是数据绑定

Now this can't be done because I get an exception saying something in the lines of you cant add rows programatically to a DataGridView that is data bound.

现在我也看过,这可以通过使用表适配器插入解决行而不是通过直接添加他们。一个DGV。但是,这也是不可能的,因为我在DGV使用自定义标题,因为现有数据是通过加盟的,所以如果我通过一个TableAdapter添加他们,我得到一个异常的不匹配的数据库表模式获取的。

Now I have also read that this can be solved by using a table adapter to insert the rows instead of directly adding them via. a DGV. But this is also not possible because I use custom headers in the DGV because existing data is fetched via JOIN's so if I add them via a TableAdapter I get an exception that the it doesn't match the databases table schema.

现在我真的失去了...任何人都知道一个(半)的优雅解决这个问题?

Now I am really lost... Anyone know of a (halfway) elegant solution to this problem?

感谢您

推荐答案

绑定你DGV到的BindingList< YourObject> ,其中 YourObject 可以是一个简单的类属性反映数据库架构。最初从数据库填充的BindingList 和DGV会自动添加 YourObject 实例到列表中。当你准备提交更改,从做的BindingList人工手动。

Bind your DGV to a BindingList<YourObject>, where YourObject can be a simple class with properties reflecting your database schema. Initially populate the BindingList from the database, and the DGV will automatically add YourObject instances to the list. When you're ready to commit the changes, do so manually from the BindingList.

这篇关于添加行编程到正在数据绑定一个DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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