Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1150
How to initialize the AddNew record by code
posted

Hello,

My grid provides the AddNew record, 
but additionally I want the user to be able to create a new record by a keyboard shortcut and a ribbon button.
In the code behind the following code is executed when the user initiates this command:


grid.ExecuteCommand(DataPresenterCommands.EndEditModeAndCommitRecord);
grid.ActiveRecord = grid.RecordManager.CurrentAddRecord;
grid.ExecuteCommand(DataPresenterCommands.CellFirstInRecord);
grid.ExecuteCommand(DataPresenterCommands.StartEditMode);
 

This commits and current editing, activates the AddNewRecord line and puts the first cell in edit mode.
So far - so good.

Unfortunately the underlying business object is not initialized by this action.
Only when the user starts typing, the object is created and initialized.
How can I bring the grid to do this initializing on its own?

Thank you for any hint. 

BTW: how can I paste code with syntax highlighting?
I even tried the paste-from-word thing, but after inserting the coloring and formatting is gone... 

  • 34510
    Offline posted

    Hello J_Feuerstein,

    By default the XamDataGrid does not create the underlying record for the AddNewRecord until there is actual data entered into the cell.  It doesn't make sense to create the item if there is no data for it.  When the user presses the keyboard shortcut or ribbon button, what is the exact behavior you are looking for?  If all you want to do is have a new record created you can do this via a view model command which then adds a new item to the underlying datasource.  If your datasource implements INotifyCollectionChanged then a new record will be added to the XamDataGrid automatically.