INotifyPropertyChanged or INotifyCollectionChanged with DataTable?
Asked Answered
K

3

5

Hi i am having some troube with DataTables. So What i need is to detect whenever i change any cell in the DataGrid of the DataTable that is binded.

How to do it? With INotifyPropertyChanged or with INotifyCollectionChanged?

Note: I am trying with INotifyPropertyChanged but it only detects when i set some value in the DataTable, and never when i change any value of any cell in the DataGrid, i already have tried OneWay and TwoWay but nothing happens.

Thanks in advance!

Khorma answered 27/4, 2011 at 3:14 Comment(0)
B
12

The datagrid would be bound to a list of objects. If you want the grid to update when individual object properties change, than each contained object must implement the INotifyPropertyChanged interface.

The INotifyCollectionChanged is an interface that the collection should implement, and are for notifications of addition and removal events.

See the section "How to implement collections" on this page.


Here's a way to approach your problem:
  • Create a new class that exposes the properties currently held in each DataRow. On this class implement INotifyPropertyChanged.
  • Instead of a DataTable, use an ObservableCollection<T> or your new class.

ObservableCollection already implements INotifyCollectionChanged, so this saves you the effort of implementing it yourself.

Blown answered 27/4, 2011 at 3:24 Comment(3)
So if i understand right, i will have to turn my DataTable in an IEnumarable, right? And then i will have to implement collections notifications when is inserted or deleted something. But without a real example of using INotifyCollectionChanged is difficult. I already have understand how to work with INotifyPropertyChanged but with collections i haven't find any good example.Khorma
@Khorma - I've added an extra paragraph to answer your concerns.Blown
dont know why someone downvote me, but i will just add something to this answer. there is no need to create a observablecollection. i use datagrid with itemssource to datatable all over in my project, i can add, modify, delete the content, have validation and filtering and this all without wrapping my datatable. btw i still dont know WHY Miguel wants to know when any cell is changed. there is a HasChanges Property which tell you when there is a change.Stound
S
0

if you set the itemssource of your datagrid to a datatable then wpf create a IBindingListView wich is bound to the datagrid.

what you can do now is edit,add and delete items to your datatable via datagrid. if you wanna know when a cell in your datatable is changed you can subscribe to DataTable.ColumnChanged event.

why do you want do know when a cell is changed?

Stound answered 27/4, 2011 at 9:41 Comment(1)
If you programaticaly modify a cell in your datatable then does your change automatically reflect in the datagrid display?Blown
S
0

The answer to the title of your question is : Neither. Actually you do not need to bind a DataTable to a DataGrid. You bind a DataView. "The ADO.NET DataView implements the IBindingList interface, which provides change notifications that the binding engine listens for."(Binding Sources Overview) One answer to your question is : You modify the datagrid cell with a TextBox (usually). Do this with a new textbox inheriting from TextBox and override the OnGotFocus and OnLostFocus methods of it.

Silverfish answered 30/9, 2020 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.