6

Im currently trying to bind the DataGrid.ItemsSource to a custom RowCollection, which implements IList and INotifyCollectionChanged:

Public Class RowCollection(of T)
Implements IList(Of T)
Implements INotifyCollectionChanged
Private _List As New List(Of T)
...

(Sorry for the VB code, I'll be translating all my code to C# soon.)

Notice the class does not derive from any existing CLR collection. I created my own class because I need to override GetItemAt, in order to implement record paging. The Collection Internally adds and removes objects from its own private List _List.

Now, I am able to view the items in a DataGrid, but as soon as I double click a cell to edit, I recieve an InvalidOperationException: 'EditItems' is not available..

My question is, what other interfaces should I implement in order to make my collection fully compatible with DataGrid?

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • +1 I remember that I had the same problem but have not tried to dig into it because I had not the time. As far as I remember, I ended up in using ObservableCollection as ItemsSource synching them with my own collections. I'm curious if someone has a solution. – HCL Mar 03 '11 at 13:52
  • I think what you are trying to do can be done in a simpler way. Use an existing collection, and then put the DataGridView into virtual mode and implementing the CellValueNeeded(object, ...CellEventArgs) handler. Link: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.virtualmode.aspx The purpose of virtual mode is to support the kind of paging scenario you have in mind. – Ritch Melton Mar 03 '11 at 14:06
  • 1
    Sorry. I forgot to mention Im using the WPF DataGrid in the WPF Toolkit for .Net 3.5. The link you mentioned is about WinForms. – Federico Berasategui Mar 03 '11 at 14:15
  • @HighCore - yes it is. Thanks for the clarification, I should have noticed that. – Ritch Melton Mar 03 '11 at 14:18
  • @HighCore: You have accepted the answer from Vincent Vancalbergh. Was it the IEditableCollectionView-interface that had to be implemented? Has it worked for you? – HCL Mar 14 '11 at 12:45
  • 1
    @HCL - Yes. Actually, I ended up using a normal ObservableCollection and created a derived ListCollectionView, where I define custom logic for retrieving items and getting item count. Therefore I was able to create some sort of data virtualizatiion. Thanks! – Federico Berasategui Mar 15 '11 at 17:17

1 Answers1

3

Here you can read the following:

Editing

By default, you can edit items directly in the DataGrid. To guarantee that edits can be committed and canceled correctly, the objects in the DataGrid must implement the IEditableObject interface. Alternatively, you can set the IsReadOnly property to true to disable editing in the DataGrid.

The IEditableObject interface is here Also see IEditableCollectionView here

Vincent Vancalbergh
  • 3,267
  • 2
  • 22
  • 25