0

I'm trying to do something rather simple, and getting frustrated by .Net:

I simply want to have a WPF DataGrid that automatically adds a blank row at the end, similar to entering data in a table in MS Access or SQL Management Studio.

My idea was to override ObservableCollection: keep a newItem member, which I dynamically add when the Items collection is read. Something like this:

// Warning this code doesn't work
public class MyCollection<T> : ObservableCollection<T>
{
    private T _newItem;

    // there would be a constructor which would initialize _newItem

    protected override IList<T> Items
    {
        get
            {

                List<T> newItems = new List<T>();
                newItems.Add(_newItem);
                return base.Items.Union(newItems).ToList();
            }
    }
}

There are several problems with my code. First, you can't override the getter of the Items property. It isn't overridable.

Second, even if you could, that getter isn't even called when a DataGrid is bound to the collection. I'm thinking the GetEnumerator method should be overridden here, but of course that's not overridable either.

What's the easiest way to extend the ObservableCollection class to implement this functionality. At this point it's looking like I'll have to implement the whole thing myself, and not inherit from ObservableCollection. For obvious reasons I'm resistant to that idea.

Thanks in advance!

FunkMonkey33
  • 1,956
  • 2
  • 16
  • 24

1 Answers1

1

As far as I know, the DataGrid does display a blank row at the bottom if you set CanUserAddRows to True and ensure your DTO class has a default constructor. Doesn't this work in your case?

In any case, you are trying to solve a UI issue by modifying your data source so it's no wonder you encounter problems. That's a clear indication that this isn't the way to go.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • I am familiar with that feature of the DataGrid, however, I don't love the UX. Really trying to duplicate the experience of Access or SQL Management Studio when editing table data. – FunkMonkey33 Dec 15 '14 at 17:22
  • All these things are UI concerns you should solve in your View or a UserControl, not the data behind it. It seems the actual question isn't how to display the new row at all but how to create a different UI, while keeping databinding simple. Eg. you could add a single-line datagrid right below the main grid, bind it to a single object and handle transferring the new line to the main *collection* in your code. Consider using another grid that already provides this functionality. A good grid probably costs a *lot* less than a programmer's salary for one week and does a lot more – Panagiotis Kanavos Dec 16 '14 at 08:24
  • In general, I agree with you. However, what is an ObservableCollection if not a specialized container for displaying data in a List Items Control? It seems like a logical next step to extend it to make a slightly-more specialized container. – FunkMonkey33 Dec 16 '14 at 16:08
  • And BTW my original question remains unanswered, which is: what's the easiest way to extend ObservableCollection (i.e. where to inherit from, what to override, etc.). If I'd left out my ultimate purpose for wanting to do this, someone may have answered the actual question. All too often, developers in SO and other online communities don't actually answer people's question, instead opting to make themselves look smart by telling people their question was stupid, naive, amateurish, etc. – FunkMonkey33 Dec 16 '14 at 16:09
  • That's not correct. A List control can bind to *any* IEnumerable. The control will update its display when the CollectionChanged or PropertyChanged events are raised, no matter what the source is. ObservableCollection is simply a collection that raises an event for each item. – Panagiotis Kanavos Dec 16 '14 at 16:10
  • As for the question, it's already been answered in SO, eg [here](http://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each). This won't make implementing the insert row easier though – Panagiotis Kanavos Dec 16 '14 at 16:21