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

Prevent Grid from Processing Return Key

Hi

I'm using SfDataGrid to display data and have disabled Editing & Deleting. I'm handling Enter & Deleted KeyUp event from vb.net code to perform stuffs

If user presses Enter Key, Selected Row Changes i.e. focus gets to next row and it gets selected. 

How can I prevent this.

Also when user double clicks on particular row I want to raise KeyUp Event with enter key. How Can I achieve this also.

Thanks in Advance 

Amit Saraf

8 Replies

GT Gnanasownthari Thirugnanam Syncfusion Team February 26, 2016 01:08 PM UTC

Hi Amit,

Please find the responses for the reported queries.

Query
Comments
I'm using SfDataGrid to display data and have disabled Editing & Deleting. I'm handling Enter & Deleted KeyUp event from vb.net code to perform stuffs
If user presses Enter Key, Selected Row Changes i.e. focus gets to next row and it gets selected.
How can I prevent this?
You can handle Enter key behavior by overriding ProcessKeyDown method in GridSelectionController classs.



this.dataGrid.SelectionController = new GridSelectionControllerExt(dataGrid);



public class GridSelectionControllerExt : GridSelectionController

{

    private SfDataGrid grid;


    public GridSelectionControllerExt(SfDataGrid datagrid)

        : base(datagrid)

    {

        grid = datagrid;

    }


    //overriding the ProcessKeyDown Event from GridSelectionController base class

    protected override void ProcessKeyDown(KeyEventArgs args)

    {          

        if (args.Key == Key.Enter)

        {            

            return;

        }

        base.ProcessKeyDown(args);

    }
}


You can refer the below KB for more information.

https://www.syncfusion.com/kb/3815/how-to-change-the-enter-key-behavior-in-sfdatagrid


Also when user double clicks on particular row I want to raise KeyUp Event with enter key. How Can I achieve this also.
It is not possible to raise key up event when double clicking. But you can override double click in selection controller and do needed action.
Please let us know if you have any other queries.

Regards,
Gnanasownthari T


AS Amit Saraf February 26, 2016 02:02 PM UTC

Thanks for your help

Regarding Enter Key Behaviour

That did just what I want.

Regarding Mouse Doubleclick

What if I try to process mouse double click event in VB.Net code behind and raise Key up event and pass enter key (I did this in my windows form control) 

(I need to repeat this frequently in my application so I have prepared a control which inherits SfDataGrid)

    Private Sub Me_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseDoubleClick
        OnKeyUp(New KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(Me), 0, Key.Enter))
    End Sub

When I double click mouse this code executes but I'm not understanding what is wrong here 

Thanks again




SR Sivakumar R Syncfusion Team February 29, 2016 04:36 PM UTC

Hi Amit,

Your code is correct. Can you please let us know the problem you are facing with this code.

You can override GridSelectionContoller also to handler double click as like in the below code snippet also.

this.dataGrid.SelectionController = new GridSelectionControllerExt(dataGrid);


public class GridSelectionControllerExt : GridSelectionController

{

    public GridSelectionControllerExt(SfDataGrid dataGrid) : base(dataGrid)

    {


    }

    public override void HandlePointerOperations(GridPointerEventArgs args, RowColumnIndex rowColumnIndex)

    {

        if (args.Operation == PointerOperation.DoubleTapped)

            this.HandleKeyDown(new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(this.DataGrid), 0, Key.Enter));


        else

            base.HandlePointerOperations(args, rowColumnIndex);

    }
}


Can you please let us know the problem you are facing. That will help us to provide better solution.

Thanks,
Sivakumar


AS Amit Saraf March 1, 2016 05:40 AM UTC

Thanks for your reply
Here is my code after updating as per suggestions 

Imports Syncfusion.UI.Xaml.Grid

Public Class GSLV
    Inherits SfDataGrid

    Public Event RefreshList()

    Dim ProcessKeys As Boolean

    Public Sub New()
        SetResourceReference(StyleProperty, GetType(SfDataGrid))
        Me.SelectionController = New GridSelectionControllerExt(Me)
    End Sub

    Public Property ProcessKeyUp As Boolean
        Set(value As Boolean)
            ProcessKeys = value
        End Set
        Get
            Return ProcessKeys
        End Get
    End Property


    Private Sub Me_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If ProcessKeys Then
            Try

                If e.Key = Key.F5 Then : RaiseEvent RefreshList() : e.Handled = True
                ElseIf (SelectedItems.Count > 0) AndAlso (e.Key = Key.Enter Or e.Key = Key.Delete Or
                                                            e.Key = Key.F8 Or e.Key = Key.F9 Or
                                                            e.Key = Key.F11 Or e.Key = Key.F12) Then

                    Dim R As System.Data.DataRow = DirectCast(SelectedItem, System.Data.DataRowView).Row
   .
   .
   . Do Something
   .
                    e.Handled = True
                End If
            Catch ex As Exception : Throw New System.Exception(ex.Message, ex.InnerException) : Finally : e.Handled = True : End Try
        End If
    End Sub

    Private Sub Me_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        'If ProcessKeys Then
        '    Try
'
        '        If e.Key = Key.F5 Then : RaiseEvent RefreshList() : e.Handled = True
        '        ElseIf (SelectedItems.Count > 0) AndAlso (e.Key = Key.Enter Or e.Key = Key.Delete Or
        '                                                    e.Key = Key.F8 Or e.Key = Key.F9 Or
        '                                                    e.Key = Key.F11 Or e.Key = Key.F12) Then
'
        '            Dim R As System.Data.DataRow = DirectCast(SelectedItem, System.Data.DataRowView).Row
'    .
'    .
'    . Do Something
'    .
        '            e.Handled = True
        '        End If
        '    Catch ex As Exception : Throw New System.Exception(ex.Message, ex.InnerException) : Finally : e.Handled = True : End Try
        'End If
    End Sub

    Private Sub Me_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseDoubleClick

    End Sub

End Class

Public Class GridSelectionControllerExt
    Inherits GridSelectionController

    Public Sub New(datagrid As SfDataGrid)
        MyBase.New(datagrid)
    End Sub

    Protected Overrides Sub ProcessKeyDown(args As KeyEventArgs)
        If args.Key = Key.Enter Then
            Return                                                                                                                                                                                                         'Event Triggered - 2
        End If
        MyBase.ProcessKeyDown(args)
    End Sub

    Public Overrides Sub HandlePointerOperations(args As GridPointerEventArgs, rowColumnIndex As RowColumnIndex)
        If args.Operation = PointerOperation.DoubleTapped Then
            Me.HandleKeyDown(New KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(Me.DataGrid), 0, Key.Enter))               'Event Triggered - 1
        Else
            MyBase.HandlePointerOperations(args, rowColumnIndex)
        End If
    End Sub

End Class


Events being triggered on mouse double click are as fellows

1. Me.HandleKeyDown(New KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(Me.DataGrid), 0, Key.Enter)) 
2.  If args.Key = Key.Enter Then Return (marked above)

Thus Mouse Double Click Event is handled in GridSelectionControllerExt Class and not transferred to key events in SfDataGrid

Thanks in advance
Amit Saraf


SR Sivakumar R Syncfusion Team March 2, 2016 02:58 AM UTC

Hi Amit,

Thanks for the code snippet shared in the previous update. Please find the modified code snippet to achieve your requirement. Now OnKeyUp is triggered in when double clicking mouse as expected.

Sample:
http://www.syncfusion.com/downloads/support/forum/123198/ze/GridSelectionController-2072735055

Code snippet:

public class GSLV : SfDataGrid

{

    protected override void OnKeyDown(KeyEventArgs e)

    {

        base.OnKeyDown(e);

    }


    protected override void OnKeyUp(KeyEventArgs e)

    {

        Console.WriteLine("Enter triggered");

        base.OnKeyUp(e);

    }


    protected override void OnMouseDoubleClick(MouseButtonEventArgs e)

    {

        this.OnKeyUp(new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(this), 0, Key.Enter));

        //base.OnMouseDoubleClick(e);

    }

}


public class GridSelectionControllerExt : GridSelectionController

{

    public GridSelectionControllerExt(SfDataGrid dataGrid) : base(dataGrid)

    {


    }


    public override bool HandleKeyDown(KeyEventArgs args)

    {

        if (args.Key == Key.Enter)

            return false;

        return base.HandleKeyDown(args);

    }


    public override void HandlePointerOperations(GridPointerEventArgs args, RowColumnIndex rowColumnIndex)

    {

        if (args.Operation == PointerOperation.DoubleTapped)

            return;

        else

            base.HandlePointerOperations(args, rowColumnIndex);

    }
}


Thanks,
Sivakumar


AS Amit Saraf March 2, 2016 05:55 AM UTC

Thanks for your reply

On changing code as per your code the OnKeyUp event triggered but with an error

An unhandled exception of type 'System.Exception' occurred in Syncfusion.Learning.exe

System.Exception was unhandled
  HResult=-2146233088
  Message=Every RoutedEventArgs must have a non-null RoutedEvent associated with it.
  Source=Syncfusion.Learning
  StackTrace:
       at Syncfusion.Learning.GSLV.OnKeyUp(KeyEventArgs e) in D:\Other Data\Tutorials & Samples\WPF\WPF Learning\Syncfusion Controls\Syncfusion\GS Listview\GSLV.vb:line 247
       at Syncfusion.Learning.GSLV.OnMouseDoubleClick(MouseButtonEventArgs e) in D:\Other Data\Tutorials & Samples\WPF\WPF Learning\Syncfusion Controls\Syncfusion\GS Listview\GSLV.vb:line 257
       at System.Windows.Controls.Control.HandleDoubleClick(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at Syncfusion.Learning.Application.Main() in D:\Other Data\Tutorials & Samples\WPF\WPF Learning\Syncfusion Controls\Syncfusion\obj\Debug\Application.g.vb:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 




AS Amit Saraf March 3, 2016 11:51 AM UTC

Hi Sivakumar R
I figured out another way of doing it
Here is code

    Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
        MyBase.OnKeyDown(e)
    End Sub

    Protected Overrides Sub OnKeyUp(e As KeyEventArgs)
        If ProcessKeys Then
            If ProcessKeyPressed(e.Key) Then e.Handled = True
        End If
        MyBase.OnKeyUp(e)
    End Sub

    Protected Overrides Sub OnMouseDoubleClick(e As MouseButtonEventArgs)
        If ProcessKeys Then
            If ProcessKeyPressed(Key.Enter) Then e.Handled = True
        End If
        MyBase.OnMouseDoubleClick(e)
    End Sub

Private Function ProcessKeyPressed(e As Key) As Boolean
        Dim KeyProcessed As Boolean = False
        Try
                ' Do required stuff here and set KeyProcessed = True if processed key
        Catch ex As Exception : Throw New System.Exception(ex.Message) : End Try
        Return KeyProcessed
    End Function

Thanks for Your Help
Amit Saraf


AP Ashwini Paranthaman Syncfusion Team March 3, 2016 12:13 PM UTC

Hi Amit,
We are glad that your issue has been fixed.
Please let us know if you need any other assistance.
Regards,
Ashwini P.

Loader.
Live Chat Icon For mobile
Up arrow icon