Commit Row on lost focus

Hello,

Is there a way to commit a new row (or the edit of a previously existing row) when the SfDataGrid lost focus (for example to click a save button)?

Now the default behavior is that the row is only commit to the underlaying ItemsSource object when the row is finished (reaching the last cell in the row and pass to the new row) but sometimes the row doesn't need to have all the cells filled and is the last to add and a lost focus could validate and commit that row.

Is there a way to do this, maybe through the LostKeyboardFocus or a better approach to it?


Thanks in advance.


3 Replies

VS Vijayarasan Sivanandham Syncfusion Team March 16, 2022 04:17 PM UTC

Hi David García,

Your requirement to commit a new row when the SfDataGrid lost focus can be achieved by customizing the LostKeyboardFocus event. Please refer the below code snippet,

 
private void OnLostKeyBoardBehavior(object sender, KeyboardFocusChangedEventArgs e) 
{            
 
            if (!this.sfDataGrid.IsKeyboardFocusWithin) 
            {                
                if (this.sfDataGrid.View != null && this.sfDataGrid.View.CurrentEditItem != null && !sfDataGrid.View.IsAddingNew) 
                { 
                    // it commit the modified value in the grid.          
                    this.sfDataGrid.View.CommitEdit();                   
                    
                } 
                if (this.sfDataGrid.View != null && this.sfDataGrid.View.IsAddingNew) 
                { 
                    // it commit the entered value in the grid. 
                    if (this.sfDataGrid.SelectionController.CurrentCellManager.CurrentCell.IsEditing) 
                        this.sfDataGrid.SelectionController.CurrentCellManager.EndEdit(true); 
                    var addNewRowController = this.sfDataGrid.GetAddNewRowController(); 
                    addNewRowController.CommitAddNew(); 
                }               
            }             
} 
Vijayarasan S




DG David García March 16, 2022 09:54 PM UTC

Thank you so much for the reply, That's exactly what I was looking for.



VS Vijayarasan Sivanandham Syncfusion Team March 17, 2022 06:07 AM UTC

Hi David García,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S


Loader.
Up arrow icon