Enable cell edit on button click and disable edit on different cell selection

Hi,

I'm trying to enable cell edit with button click, and upon pressing enter or selecting a different row the edit cell should be disabled.
 
My SfDataGrid contain:

AllowEditing="False"  
SelectionMode="Single"                                             
NavigationMode="Row"


Upon click I set:

 int rowNumber = WaveformGalleryDataGrid.SelectedIndex;

            if( rowNumber != -1 )
            {
                    WaveformGalleryDataGrid.NavigationMode = Syncfusion.UI.Xaml.Grid.NavigationMode.Cell;

                    WaveformNameHeader.AllowEditing = true;

                    RowColumnIndex rowColumnIndex = new RowColumnIndex( rowNumber + 1, 0 );
                    WaveformGalleryDataGrid.MoveCurrentCell( rowColumnIndex );
                    WaveformGalleryDataGrid.SelectionController.CurrentCellManager.BeginEdit();

                    WaveformGalleryDataGrid.SelectionChanging += waveformGalleryDataGrid_SelectionChanging;
               }

Now I can edit my desired cell.


Upon pressing on the enter or selecting a different cell event is fire:


 void waveformGalleryDataGrid_SelectionChanging( object sender, GridSelectionChangingEventArgs e )
        {
            if( editWaveformName == true )
            {
                WaveformGalleryDataGrid.NavigationMode = Syncfusion.UI.Xaml.Grid.NavigationMode.Row;

                WaveformNameHeader.AllowEditing = false;

                WaveformGalleryDataGrid.SelectionController.CurrentCellManager.EndEdit();

                WaveformGalleryDataGrid.SelectionChanging -= waveformGalleryDataGrid_SelectionChanging;
               
            }
          
        }


My problem is that on the last row if I press enter nothing happen, the event "waveformGalleryDataGrid_SelectionChanging" won't fire.

I tried to change the enter behavior using the following code link
without success.

Is there any way to solve that?

The first column in my SfDataGrid is a file name.
And I would like to allow the user to change the default file name.


Regards,

Dov.








1 Reply

MK Muthukumar Kalyanasundaram Syncfusion Team February 12, 2018 06:17 PM UTC

Hi Dov, 

You can achieve your requirement by setting NavigationMode as Cell and override the ProcessKeyDown event in this GridCellSelectionController class as like below code. Please refer the below code snippet. 

Code Snippet: Xaml 

<syncfusion:SfDataGrid x:Name="dataGrid" 
                        Grid.Column="0" 
                        AllowEditing="False" 
                        SelectionMode="Single" 
                        NavigationMode="Cell" 
                        ColumnSizer="Star" 
                        AutoGenerateColumns="False" 
                        LiveDataUpdateMode="AllowDataShaping" 
                        ItemsSource="{Binding UserDetails}"                                                               
                        ShowRowHeader="False"> 


Code Snippet: C# 
 
public class GridSelectionControllerExt : GridCellSelectionController 
{ 
    public GridSelectionControllerExt(SfDataGrid datagrid) 
        : base(datagrid) 
    { 
    } 
 
    //overriding the ProcessKeyDown Event from GridSelectionController base class 
    protected override void ProcessKeyDown(KeyEventArgs args) 
    { 
        if (args.Key == Key.Enter) 
        { 
            KeyEventArgs arguments = new KeyEventArgs(args.KeyboardDevice, args.InputSource, args.Timestamp, Key.Tab) { RoutedEvent = args.RoutedEvent }; 
            base.ProcessKeyDown(arguments); 
            //assigning the state of Tab key Event handling to Enter key 
            args.Handled = arguments.Handled; 
            return; 
        } 
        base.ProcessKeyDown(args); 
    } 
} 
 
// Select button to edit the first column of the grid. 
 
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    int rowNumber = WaveformGalleryDataGrid.SelectedIndex; 
    int colIndex = WaveformGalleryDataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex; 
    if (rowNumber != -1 && colIndex == 0) 
    { 
        WaveformGalleryDataGrid.AllowEditing = true; 
        RowColumnIndex rowColumnIndex = new RowColumnIndex(rowNumber + 1, 0); 
        WaveformGalleryDataGrid.MoveCurrentCell(rowColumnIndex); 
        WaveformGalleryDataGrid.SelectionController.CurrentCellManager.BeginEdit(); 
        // while navigating current cell to next row or cell, the CurrentCell event will be triggerd                 
        WaveformGalleryDataGrid.SelectionChanging += WaveformGalleryDataGrid_SelectionChanging; 
    } 
}  



For more details about current cell navigation, you can refer the below link, 
 
Please let us know if you have any concerns. 

Regards, 
Muthukumar K 


Loader.
Up arrow icon