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

DataGrid RightTapped Behaviour

Hi,

when I was using the Community Toolkit doing a right click was not automatically selecting a cell/row.


Now I'm using SfDataGrid with with SelectionMode="Extended" and a context menu. My problem right here is, that when selecting multiple items in my datagrid and doing a right click, it will deselect the rest of my previously selected items and only select the currently right clicked item. What I want to achieve is, that when multiple items are selected those items stay selected, so I can do some action my context menu like deleting those items.

I tried using

public override void HandlePointerOperations(GridPointerEventArgs args, Syncfusion.UI.Xaml.Grids.ScrollAxis.RowColumnIndex rowColumnIndex)

but args.Operations only handles left clicks.

Bascially what I want to achieve is, that a right click only selects a new item, when sfDataGrid.SelectedItems.Count <= 1, otherwise just open the context menu without (de-)selecting the current right clicked item.


Best Regards

Tom


3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team October 17, 2022 01:44 PM

Hi Thomas Potzel,


Solution 1:


If your SfDataGrid applied RowSelection. Your requirement can be achieved by overriding the ProcessPointerPressed method in GridSelectionController in SfDataGrid. Please refer to the below code snippet,

//set the customized GridSelectionControllserExt to SfDataGrid.SelectionController when RowSelection applied in SfDataGrid

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

 

 //Inherits the GridSelectionController Class

 public class GridSelectionControllerExt : GridSelectionController

 {

     public GridSelectionControllerExt(SfDataGrid dataGrid) : base(dataGrid)

     {

     }

 

     //overriding the ProcessPointerPressed method from GridSelectionController base class

     protected override void ProcessPointerPressed(PointerRoutedEventArgs args, RowColumnIndex rowColumnIndex)

     {

         //Here customize based on your scenario

 

         //here get the PointerPointProperties

         var properties = args.GetCurrentPoint(DataGrid).Properties;

 

         //Here check the custom condition based on your scenario

         if (properties.IsRightButtonPressed && !(DataGrid.SelectedItems.Count <= 1))

         {

             //Here handled the Selection

             args.Handled = true;

         }

         else

             base.ProcessPointerPressed(args, rowColumnIndex);           

     }

 }

 


UG Link: https://help.syncfusion.com/winui/datagrid/selection#customizing-selection-behaviors


Solution 2:


If your SfDataGrid applied CellSelection. Your requirement can be achieved by overriding the ProcessPointerPressed method in GridCellSelectionController in SfDataGrid. Please refer to the below code snippet,

//set the customized GridCellSelectionControllserExt to SfDataGrid.SelectionController when CellSelection applied in SfDataGrid

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

 

//Inherits the GridCellSelectionController Class

public class GridCellSelectionControllerExt : GridCellSelectionController

{

    public GridCellSelectionControllerExt(SfDataGrid datagrid)

      : base(datagrid)

    {

    }

 

    //overriding the ProcessPointerPressed method from GridCellSelectionController base class

    protected override void ProcessPointerPressed(PointerRoutedEventArgs args, RowColumnIndex rowColumnIndex)

    {

        //Here customize based on your scenario

 

        //here get the PointerPointProperties

        var properties = args.GetCurrentPoint(DataGrid).Properties;

 

        //Here check the custom condition based on your scenario

        if (properties.IsRightButtonPressed && !(DataGrid.SelectionController.SelectedCells.Count <= 1))

        {

            //Here handled the Selection

            args.Handled = true;

        }

        else

            base.ProcessPointerPressed(args, rowColumnIndex);

    }

}



UG Link: https://help.syncfusion.com/winui/datagrid/selection#customizing-selection-behaviors


Please find the sample in the attachment and let us know if you have any concerns about this.


Regards,

Vijayarasan S

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



Attachment: SfDataGridDemo_1ffaad0e.zip

Marked as answer

TP Thomas Potzel October 18, 2022 12:05 PM

Thank you very much, exactly what I've been trying to achieve :)



VS Vijayarasan Sivanandham Syncfusion Team October 19, 2022 11:57 AM

Hi Thomas Potzel,

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.
Live Chat Icon For mobile
Up arrow icon