SfDataGrid - Prevent right click from selecting row

Hi,

Is there any way to prevent the right click from changing row selection in a SfDataGrid? I just want to use it for context menus.

3 Replies

JG Jai Ganesh S Syncfusion Team April 22, 2016 05:13 AM UTC

Hi Arvin,

You can prevent the right click from changing row selection in SfDataGrid by wiring the PreviewMouseDown event like below,

this.SfdataGrid.PreviewMouseDown+=SfdataGrid_PreviewMouseDown;


void SfdataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)

        {

            if(e.ChangedButton==MouseButton.Right)

            {

                e.Handled = true;

            }

        }


Regards,

Jai Ganesh S



AR Arvin May 4, 2016 11:15 PM UTC

There doesn't seem to be an event called PreviewMouseDown. 

I have a datagrid declared in XAML

<syncfusionGrid:SfDataGrid x:Name="TrackListUi"  ...

in my page's constructor I try to subscribe to the event as you suggested but PreviewMouseDown isn't one of the events in the list. Any suggestions?


JG Jai Ganesh S Syncfusion Team May 5, 2016 02:44 PM UTC

Hi Arvin, 
 
Sorry for the inconvenience. 
You can prevent the right click from changing row selection in SfDataGrid by customizing the SelectionController and overriding the ProcessPointerPressed like below, 
this.datagrid.SelectionController = new GridSelectionControllerExt(datagrid); 
 
public class GridSelectionControllerExt : GridSelectionController 
    { 
 
        public GridSelectionControllerExt(SfDataGrid dataGrid) 
            : base(dataGrid) 
        { } 
 
        protected override void ProcessPointerPressed(PointerRoutedEventArgs args, RowColumnIndex rowColumnIndex) 
        { 
            var properties = args.GetCurrentPoint(DataGrid).Properties; 
            if (properties.IsRightButtonPressed) 
            { 
                args.Handled = true; 
            } 
            else 
                base.ProcessPointerPressed(args, rowColumnIndex); 
        } 
    } 
 
Sample
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon