This is a migrated thread and some comments may be shown as answers.

Questions about new records and MVVM

0 Answers 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 24 Jan 2012, 06:56 PM
I have a Telerik GridView that I am using in MVVM mode. That is, I've not implemented event handlers in the code-behind, rather I've bound it to an ObservableCollection.  And I have two problems.

When I click on the "Click here to add new item" bar in the grid, the CollectionChanged event on the collection fires, with it's Action set to NotifyCollectionChangedAction.Add.  Which is what I would expect to happen.  Then after the handler returns, the new blank row appears at the top of the grid.

The first problem: it's the second cell that is selected, and actively editable, not the first.

How do I configure the editor to have the first cell selected?  It clearly can't be in the ViewModel - the observable collection doesn't have access to the grid - and shouldn't.  Is there something I can add to the XAML that would control which field is active, when editing?

(One thing that might be relevant - the first cell is only editable when adding a new record, it's read-only when editing an existing record.  The GridViewDataColumn has its IsReadOnlyBinding property bound to a property in the viewmodel that is true when editing an existing record.  So when editing an existing row, I want to start editing with the second cell, when adding a new a new row I want to start editing with the first.)

The second problem: updating the data.

The rows in the grid are bound to elements in the collection, and those elements implement INotifyPropertyChanged.  So when I am editing a record in the grid, after I leave each file, the propertyChanged event fires on the element.  In that, I can update the underlying data store - except that I don't want to.  I don't want to insert or update the record, after every field is changed, I want to do one insert or update when the row is complete. 

But I can't figure out what event would indicate that editing of the row is complete.  PropertyChanged is fired on the element as every field is changed, it doesn't tell me when the user is done editing a row.  And Collection changed is fired when a new element is added to the collection, it doesn't fire to tell me that one of the elements has changed.

So, how do folks usually handle this?

== addendum ==
For what it's worth, I've tried adding an AddingNewDataItem handler:
void customerAttributesGrid_AddingNewDataItem(object sender,
        telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
{
    var grid = e.OwnerGridViewItemsControl;
    grid.CurrentColumn = grid.Columns[0];
}
It's not helped.  The event fires, I can walk through the handler in the debugger, setting CurrentColumn as suggested, then the CollectionChanged event fires and I walk through that.  But when I'm done, the second column is still current.

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Jeff
Top achievements
Rank 1
Share this question
or