C# 在 WinForms 中更新 BindingSource 不会更新数据源集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14571451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating of BindingSource in WinForms does not update Datasource Collection
提问by Jeff Clare
I want to display a custom collection in a DataGridView
in a Windows Forms app. This custom collection implements ICollection
, and IEnumerable
. I have set up a BindingSource
, using the collection as the .DataSource property. The DataGridView
is set to use my BindingSource
as it's DataSource. When I add a new item to the collection using the BindingSource.Add()
method, the DataGridView
updates correctly with the new item. The BindingSource
DataSource, on the other hand, does not:
我想DataGridView
在 Windows 窗体应用程序中显示自定义集合。此自定义集合实现ICollection
, 和IEnumerable
。我已经设置了一个BindingSource
, 使用集合作为 .DataSource 属性。将DataGridView
被设置为使用我的BindingSource
,因为它的数据源。当我使用该BindingSource.Add()
方法向集合添加新项目时,新项目会DataGridView
正确更新。该BindingSource
数据源,在另一方面,没有:
MyCustomCollection myCollection = new MyCustomCollection();
myCollection.Add(myCustomObject1);
myCollection.Add(myCustomObject2);
myBindingSource.DataSource(myCollection);
myBindingSource.Add(myCustomObject3);
In the above code, myBindingSource's internal List contains the right number of records (3), and the DataGridView
also contains three records, but myCollection contains only two records. I know that changing the underlying myCollection will NOT update the BindingSource
or the DataGridView
, as it is not a BindingList<T>
, but I was under the impression that updating a BindingSource
directly would ensure that myCollection was updated at the same time.
在上面的代码中,myBindingSource 的内部List 包含正确数量的记录(3),并且DataGridView
也包含三个记录,但是myCollection 只包含两个记录。我知道更改基础 myCollection 不会更新BindingSource
或DataGridView
,因为它不是 a BindingList<T>
,但我的印象是BindingSource
直接更新 a将确保同时更新 myCollection 。
Is there a way to use a collection that isn't a BindingList<T>
and have it updated when interacting with the BindingSource
directly?
有没有办法使用不是 a 的集合BindingList<T>
并在与BindingSource
直接交互时更新它?
Update: One way I've gotten the data updated across all the parts (Collection, BindingSource, DataGridView) is as follows:
更新:我在所有部分(Collection、BindingSource、DataGridView)中更新数据的一种方法如下:
myCollection.Add(myCustomObject3);
myBindingSource.DataSource = null;
myBindingSource.DataSource = myCollection;
I'm pretty sure there's a better way to attack this problem, but this is the only method that's generated the results I was expecting.
我很确定有更好的方法来解决这个问题,但这是产生我预期结果的唯一方法。
采纳答案by Frank
The problem is Fill Adaptor. When you load your form, the Fillis done for you. Just make sure to do a Refill and then follow up with Reset bindings post any data changes and Grid will get refreshed.
问题是 Fill Adapter。当您加载表单时,填写已为您完成。只需确保进行重新填充,然后在任何数据更改后跟进重置绑定,网格就会刷新。
Example :
例子 :
WorkTableAdapter.Insert(objData.XAttribute, "",
objData.YAttribute,objLoanData.Amount_IsValid, DateTime.Now, DateTime.Now);
this.WorkTableAdapter.Fill(this.POCDataSet.Work);
this.WorkBindingSource.ResetBindings(false);
回答by Eric J.
You will have to manually call ResetBindings() after the data source changes if you use a container that cannot do that on your behalf.
如果您使用的容器无法代表您执行此操作,则必须在数据源更改后手动调用 ResetBindings()。
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings.aspx
Causes a control bound to the BindingSource to reread all the items in the list and refresh their displayed values.
使绑定到 BindingSource 的控件重新读取列表中的所有项目并刷新它们显示的值。
回答by Aaron Anodide
I believe I ran into this issue a while ago - I did a find in files on my code and I think this is the solution that worked for me.
我相信我不久前遇到了这个问题 - 我在我的代码文件中做了一个查找,我认为这是对我有用的解决方案。
// Applies pending changes to the underlying data source.
this.bindingSource1.EndEdit();
This was in the context of a click handler for the save button.
这是在保存按钮的单击处理程序的上下文中。
回答by tofo
Resetting an individual item works!
重置单个项目有效!
I had no luck with .ResetBindings(false) and re-assigning the datsource caused flickering with potentiail overhead if only one item change frequently.
我对 .ResetBindings(false) 不走运,如果只有一项频繁更改,重新分配数据源会导致闪烁和潜在开销。
I tried the built in mechanism using PropertyChanged but nothing updated.
我使用 PropertyChanged 尝试了内置机制,但没有更新。
Reseting an individual item using ResetItem() worked!
使用 ResetItem() 重置单个项目有效!
for (int i = 0; i < bindingSource1.Count; i++)
{
bindingSource1.ResetItem(i);
}
And even better - if you have an update event attached to each data item in the bindningsource you can locate the object in the bindning source and use the index of the object to call ResetItem(idx)
甚至更好 - 如果您有一个更新事件附加到 bindningsource 中的每个数据项,您可以在绑定源中找到该对象并使用该对象的索引来调用 ResetItem(idx)
In this case my custom event args contains a dictionary key to the data object contained in a separate collection. After object is located in using bindningsource.IndexOf() it is individually refreshed.
在这种情况下,我的自定义事件 args 包含一个指向包含在单独集合中的数据对象的字典键。在使用 bindningsource.IndexOf() 定位对象后,它会被单独刷新。
void Value_PropertyChanged(object sender, RegisterEventArgs e)
{
var idx = bindingSource1.IndexOf(registers_ref[e.registerID]);
if (idx>=0)
{
bindingSource1.ResetItem(idx);
}
}