Unknown Coder
Unknown Coder

Reputation: 6741

Datagrid results do not make sense

I've been working with the DataGrid in WPF with great results. However, it is now giving me unexpected results after some changes.

BEFORE: I had a DataGrid on a page. The DataContext was set to a List object that was created from a class that existed within the same WPF project. The empty row at the bottom of the DataGrid, to add new records, is visible

AFTER: Same page, same DataGrid. But now the List object is coming from a Class Library project within the same solution. EXACT same code, but it's now been extracted into a class library. The empty row at the bottom of the datagrid, to add new records is not visible.

WTF?

Upvotes: 0

Views: 196

Answers (4)

bitbonk
bitbonk

Reputation: 49649

Maybe it is some security issue or even a bug. I just read this:

I found that if you access the CanAddRow of ListCollectionView once before you use the collection, magically the CanUserAddRows of the DataGrid becomes true. Strange!

IEditableCollectionView ecv = new ListCollectionView(myRecordCache);
bool b = ecv.CanAddNew;   // dummy access
MyGrid.DataContext = ecv;

Upvotes: 0

Unknown Coder
Unknown Coder

Reputation: 6741

I think I finally have the answer. Basically, I was mistaken, I did change a tiny portion of the class. The "lightbulb" went on when I read the answers to this one: How do I create a new row in WPF DataGrid when it is bound to an XmlDataProvider?

Bottom Line: The class you are binding to needs to have a default constructor in order to display an editable row!

In my code, I did change the constructors (I completely forgot about that) which left no default. Adding the default constructor back into the class fixed the problem.

Upvotes: 4

Maurizio Reginelli
Maurizio Reginelli

Reputation: 3222

I encountered the same problem when I set the DataGrid property IsReadOnly="True". Check if you have the same setting and try to remove it to see what happens.

Upvotes: 0

bitbonk
bitbonk

Reputation: 49649

What kind of list is it? Does its publically visible interface allow to add items or is it a readonly list now (e.g. IEnumerable, ICollection?

Upvotes: 0

Related Questions