How to get only modified Items from WPF Data Grid

帅比萌擦擦* 提交于 2019-12-13 03:24:43

问题


I have binded a list with Editable WPF DataGrid

I want to get only the modified Items from the WPF Data Grid for updation.

Please let me know how to do this.


回答1:


I have my WPF datagrid app set up as so:

1) Manually defined all the columns, set their bindings and for each binding specified that it notifies on source updates

2) Added an event handler for the datagrid's SourceUpdated event

3) In the event handler I have only the following code:

this.updatedItems.Add(BindingOperations.GetBindingExpression(e.TargetObject, e.Property).DataItem as MyClass); 

updatedItems is a HashSet<> and MyClass is the type of objects bound to the datagrid's ItemsSouce.

4) When the user clicks on the save button I simply enumerate the hashset. Hashset is nice, because it only contains unique values. So if a row is edited multiple times or even if multiple cells in the same row are edited, the hashset will still only contain a single entry for the object.




回答2:


Well there is a RowState property on your data row (there is also a RowChanged event), but make sure you need this. Am I under the wrong assumption that databinding automatically figures out which rows need to be updated?



来源:https://stackoverflow.com/questions/3550765/how-to-get-only-modified-items-from-wpf-data-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!