BindingSource,DataGridView,DataTable - 陷阱添加新行 [英] BindingSource, DataGridView, DataTable - Trap Adding New Row

查看:64
本文介绍了BindingSource,DataGridView,DataTable - 陷阱添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,

这个标题很简单。 我想"陷阱" "创造"的过程当DGV绑定到DataSource是DataTable的BindingSource时,DataGridView的新行。 

The title is pretty straight forward on this one.  I would like to "trap" the process which "creates" a new row for a DataGridView, when the DGV is bound to a BindingSource whose DataSource is a DataTable. 

通常,任何其他IListSource附加到BindingSource,人们只会陷阱BindingSource.AddingNew()事件,并定义e.NewObject的值。 但是,正如MSDN文档所述:

Normally, with any other IListSource attached to the BindingSource, one would simply trap the BindingSource.AddingNew() event, and define the value for e.NewObject.  However, as the MSDN documentation states:

绑定到DataView或DataTable时无法设置NewObject属性,因为您无法将新的DataRowView添加到列表中。
You cannot set the NewObject property when bound to a DataView or DataTable because you cannot add a new DataRowView to the list.

问题因此很复杂,因为当DataGridView.AllowUserToAddRows设置为true,并且DGV的DataSource是BindingSource时,"Add New Row"因此,过程完全封装在后端。  DGV转到BindingSource
并添加新项目。  BindingSource使用内部反射来创建ListSource的基础项类型,在这种情况下恰好是DataRowView,由于CurrencyManager,因此当AddingNew事件被触发时,
该项目尚未创建,但由于我们在这种情况下创建的项目是强类型DataRow后代,因此绑定源期望DataRowView,其内部DataRow属于强类型后代,因此会出现错误。

The issue is thus complicated because when DataGridView.AllowUserToAddRows is set to true, and the DataSource of the DGV is a BindingSource, the "Add New Row" process is thus ENTIRELY encapsulated in the back end.  The DGV turns to the BindingSource and adds the new item.  The BindingSource uses the internal reflection to cause the creation of the ListSource's underlying item type, which in this case happens to be a DataRowView, due to the CurrencyManager, and thus when the AddingNew event is trigger, the item is not yet created, but since the item we'd create in this circumstance is a Strongly Typed DataRow decendent, an error will arrise because the binding source is expecting a DataRowView, who's internal DataRow is of the Strongly Typed Decendent.

非常混乱,非常讨厌。 那么,有什么办法可以"陷阱"吗?这个过程,以便我可以将带有ALlowUserToAddRows的DGV设置为true,但是当请求进入为我的表创建基础DataRow时,我可以在
自己的自定义初始化中创建它等等。

Very confusing, and very annoying.  So, is there any way to "Trap" this process, so that I can have the DGV with ALlowUserToAddRows set to true, but when the request comes in to create the underlying DataRow for my table, I can created it within my own custom initializations and such.

谢谢

Jaeden" Sifo Dyas" al'Raec Ruiner

Jaeden "Sifo Dyas" al'Raec Ruiner

 

推荐答案

我不知道它的真实与否,但我是我认为我有同样的问题:)

i don't know if its true or not but i  think i have the same problem :)

 

我的问题是当我在数据网格中添加新行时,我绑定了boundingsource按下输入添加的行消失但按下更新按钮后(更新按钮生成dataadpter.update(datatable))所以在添加后出现行但是当我插入它时隐藏了

my problem is when i add new row in the data grid that bounded with boundingsource when i press enter the added row is disappear but after press update button(the update button make dataadpter.update(datatable) )so rows appear after adding but was hidden when i insert it

 

你有相同的或我不明白你发布的内容???

do you have the same or i didn't understand what you post???


这篇关于BindingSource,DataGridView,DataTable - 陷阱添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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