DataGrid - trying to create autoedit mode

Aug 5, 2009 at 6:58 AM

I'm trying to use the datagrid for some bulk data entry - for this to be usable, I need a few things which don't seem to be supported natively:

  1. I need cells to switch to edit mode as soon as the user selects them, or to be in edit mode already.
  2. I need the user to be able to tab between cells entering data, specifically I need them to be able to enter a _new_ row and then tab off the end to create another new row.

This doesn't seem like a big ask, but I'm struggling to get this working without introducing bugs. 

For 1, I can call 'BeginEdit()', but the only place this seems to work from is a Focus handler added on the DataGridCell, this has a side-effect that whenever I right click on the row header, the first cell in the table gets focus for a moment and will flicker to editing mode and back to view mode. And cells no longer commit and move to the cell below when I press 'enter'. All other places I've tried to add a 'BeginEdit()' have failed for various reasons, usually the DataGrid sets the cell IsEditing to false somewhere later on in the chain of event handling or it creates an infinite loop where beginedit causes the event to fire again. 

So, what's the correct way to get a cell to automatically switch to editing mode when selected?

For 2, the problem is that the placeholder item at the bottom of the table is removed when you start editing and not put back until the row is committed, which is _after_ the user has tabbed off the last cell in the row. I can override the datagrid's "OnExecutedCommitEdit" to force the commit to commit the whole row:

        protected override void OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
        {
            if ((e.Parameter is DataGridEditingUnit) && (DataGridEditingUnit)e.Parameter == DataGridEditingUnit.Row)
            {
                base.OnExecutedCommitEdit(e);
            }
            else
            {
                DataGrid.CommitEditCommand.Execute(DataGridEditingUnit.Row, this);
            }
        }

but I'm a bit worried that this might introduce some bugs that I just haven't noticed yet.

Cheers,

Leighton...