We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

End SfDatagrid edition when losing focus

Hi. I'm working on an app which has many SfDataGrids, each one inside an expander, and many of these expanders in different tabs.
I have some behaviour on the CurrentCellEndEdit event for every SfDataGrid but what I'm trying to accomplish is to prevent two or more SfDataGrids being in edit mode at the same time. Moreover, I need to force end edit when the user switches to a different tab than the current (this is the same way Excel finishes edition of a cell when you click in another worksheet).
I have tried to do it on LostFocus event, but for some reason this event is triggered a couple times when the edition is starting, and then one when the grid is actually losing focus. I guess this has to do with the editing textbox control gaining focus and the main element grid "losing it".
The problem is that I haven't found a way to distinguish the "real" LostFocus (the last one) which is the one in which the grid is actually losing the focus. This
grid.SelectionController.CurrentCellManager.CurrentCell.IsEditing
is true in at least one of the first triggers of the LostFocus event when the edition is beginning.

Is there any way to force the grid to end edit when losing focus? Maybe I'm taking the wrong approach trying to do it in the LoseFocus event.
Thanks in advance.

Fernando

3 Replies

JS Jayapradha S Syncfusion Team February 10, 2016 12:41 PM UTC

Hi Fernando,

Thank you for using Syncfusion products.

In TabControl.SelectionChanged event, you can end edit the current cell of SfDataGrid when swithching to different tab item.. It is not possible to achieve this by using Lost focus event. As each time the focus is changed between tab items, sfdatagrid and its elements. Please try with the below code,


Code Example:

tabControl.SelectionChanged += tabControl_SelectionChanged;     

    

void tabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)

        {

            var removedItems = e.RemovedItems;

            foreach(var removedItem in removedItems)

            {

                var expander = (removedItem as TabItem).Content as Expander;

                var grid = expander.Content as SfDataGrid;

                if (grid != null && grid.SelectionController.CurrentCellManager.CurrentCell != null && grid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)

                    grid.SelectionController.CurrentCellManager.EndEdit();

            }
        }

                                                           
For your reference, have attached the sample below,

Sample Link:
http://www.syncfusion.com/downloads/support/forum/121932/ze/SfDataGridSample1922007349

Please let us know if you have any query.

Regards,

Jayapradha


CF Carlos Fernando Consigli May 1, 2016 09:08 PM UTC

Thanks for your answer and sorry for the delay updating, I had to put aside this issue for a while.
The problem is that not only I have to end edition when moving to another tab, but also when switching focus to another grid in the same tab and even when clicking in some ribbon buttons the application has.
But I will try to apply your solution adapting that code and calling it in all focus events of components that should trigger end of edition and see if it works.


SV Srinivasan Vasu Syncfusion Team May 3, 2016 06:21 PM UTC

Hi Fernando, 
 
Sorry for delay caused. 
 
We have analyzed your query. You can achieve your requirement using SfDataGrid.LostKeyboardFocus event in SfDataGrid where the event will be raised when losing the Keyboard focus from SfDataGrid. While using ribbon buttons, the focus will be remained same in SfDataGrid which is the default behavior. Hence the lost focus will not works for ribbon buttons. Please refer the below code example. 
 
C# 
// KeyBoard Lost Focus Event 
 
  this.datagrid.LostKeyboardFocus += DataGrid_LostKeyboardFocus; 
 
void dataGrid1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) 
        { 
            if (!this.dataGrid1.IsKeyboardFocusWithin) 
            { 
                this.dataGrid1.SelectionController.CurrentCellManager.EndEdit(); 
            } 
        } 
 

 
Please download the sample from below location. 
 
 
Regards, 
Srinivasan 
 


Loader.
Live Chat Icon For mobile
Up arrow icon