将 DataGridViewRow 添加到 BindingSource
我有两个 DataGridView,一个通过 BindingSource 绑定数据,第二个 DataGridView 仅包含一行且未绑定。
在第二个 DataGridView 中,用户可以填写值,并通过“添加”按钮将值添加到第一个 DataGridView 中。
但现在我不知道如何将这个新行(记录)添加到第一个 DataGridView 中。我认为最好的办法是将这个新行添加到 DataGridView 1 的绑定源中?
但我该怎么做呢。
有人可以帮助我吗?我需要 C# 代码 谢谢了。
I have two DataGridViews, one with data bind by a BindingSource and the second DataGridView with only one row and is not bind.
In the second DataGridView users can fill in values and via an Add button the values will be Added into the first DataGridView.
But now I don't know how I have to add this new row (record) to the first DataGridView. The best thing to do this, I think is to add this new row to the bindingSource of DataGridView 1?
But how do I have to do this.
Can somebody help me? I need the code in C#
Thx already.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很典型的是,当你提出问题时,你不会得到答案,而只会得到其他“为什么”问题。我的问题很清楚,但为什么我用这个而不是那个......
所以这花了我一些时间,但最终找到了我自己的答案,使用以下内容:
其中 bs = BindingSource,RowArray 是第二个网格中的记录。
It's very typical that, when you ask an question you don't get answers but only other "why" questions. Mine question was very clear, but why I use this and not that.....
So it cost me some time but finally a find mine own answer, use the folowwing:
where bs = the BindingSource and RowArray the record from the second grid.
您可以将 BindingList 与您的业务对象一起使用。正如我在 MSDN 中看到的,.NET v2.0 支持它。你需要做什么:
You can use BindingList with your business objects. As I see in MSDN its supported in .NET v2.0. What you need to do:
为什么不设置
AllowUserToAddRows = True
并仅使用一个 DataGridView?在这种情况下,您不必负责将行添加到 DataGridView。Why don't you set
AllowUserToAddRows = True
and use only one DataGridView? In this case you don't have to take care of adding the row to the DataGridView.为什么不将数据存储在
ObservableCollection
中?将您的 DataGridView1 绑定到该字典。如果在 DataGridView2 中添加了新记录,只需向该 ObservableCollection 添加一个新条目即可。 “神奇”它将显示在 DataGridView1 中。 “The Magic”是由 ObservableCollection 实现并由 DataGridView 使用的可观察模式。Why don't you store your data in an
ObservableCollection<Type>
? Bin d your DataGridView1 to that dictioary. If then in DataGridView2 a new record is added, just simply add a new entry to that ObservableCollection. "By Magic" it will be shown in DataGridView1. "The Magic" is the observable pattern implemented by the ObservableCollection and used by the DataGridView.