1

I'm a little stuck on an issue with WPF datagrids, and once again my google foo has failed me, what i want to do seem very simple but i cant for the life of me work it out.

Please bare in mind that i am using Caliburn Micro for this application

I have a datagrid, which is bound to an ObservableCollection<Item> the items themselves handle changes using NotifyOfPropertyChange and a IsDirty flag, so editing these items is not an issue, however i cannot work out how to handle new items being added, we use the CanUserEditRows property of the datagrid to allow inline adding of new items.

However my problem comes when i try and detect a new item being added, in order to fire it off to my database services, it seems many people use the CollectionChanged event of the ObservableCollection in order to detect this, However this seem to fire the instant the user clicks on the {NewItemPlaceholder} part of the datagrid, before any data has been inserted.

private void ItemList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems != null)
    {
        foreach (Item i in e.NewItems)
        {
            _itemManager.Insert(i);
        }
    }
}

This is the code i'm using, perhaps i've made a mistake, but i cant seem to work out how to get this to fire only AFTER the editing has finished, unfortunately google seems to be returning the same results no matter how i try and reword my question. Hopefully someone here can provide me with a relatively simple answer to this question.

If you need more code to show you how things work (such as the _itemManager) I can provide if needed.

1 Answer 1

3

It's very simple:

// instances of this type user should edit in data grid
public class Item : IEditableObject
{
    // the item identifier
    public int Id { get; set; }

    // some data-bound properties

    #region IEditableObject Members

    public void BeginEdit()
    {            
    }

    public void CancelEdit()
    {
    }

    public void EndEdit()
    {
        // new items has identifier, set to 0
        if (Id == 0)
        {
            // post changes here
        }
    }

    #endregion
}

DataGrid knows about IEditableObject, and if the bound data item implements this interface, grid calls corresponding methods during the editing.

8
  • Editing the item is not my problem, Editing is working fine, its the ability to tell when a NEW item has been added to datagrid using the {NewItemPlaceholder} on the datagrid itself, would your solution not fire on every single edit, regardless on if the item is new or not?
    – Ben
    Aug 20, 2013 at 10:41
  • "when a NEW item has been added" - it's trivial, since you should have a criteria for "new item". Updated the answer.
    – Dennis
    Aug 20, 2013 at 10:49
  • Thanks for the reply, that makes sense however i have a quick question, the CRUD functions are handled by _itemManager in my viewmodel, now i'm considering just having a IsNew property on my object and doing a check the same way i use IsDirty on saving. However i wondered if there was a cleaner way that having to check both isnew and isdirty on each item when it comes to saving the data.
    – Ben
    Aug 20, 2013 at 11:22
  • Are you saving item immediately after editing, or you're using batch update to save multiple items at once?
    – Dennis
    Aug 20, 2013 at 11:39
  • 1
    Personally, I prefer approach with the enum like ItemState (New, Dirty, Unchanged) and a single state property in editable item instead of IsNew and IsDirty.
    – Dennis
    Aug 20, 2013 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.