How can I select a row on right click?

How can I select a row on right click? I just saw this thread where this behavior was marked as a bug and fixed: https://www.syncfusion.com/forums/189498/how-to-disable-selection-when-mouse-right-click.

However, I want to highlight the row when I do a right click, how can I replicate this behavior on newer versions of the library?


1 Reply 1 reply marked as answer

AP Abinesh Palanisamy Syncfusion Team September 23, 2024 09:53 AM UTC

Hi Pablo,


In SfDataGrid, direct support for right-click selection is not available. However, there is a workaround to achieve this behavior using the onCellSecondaryTap event. This event is triggered when a cell is tapped with a secondary button (right-click). We can implement the selection logic by using SfDataGrid.controller along with the rowIndex and columnIndex details from the onCellSecondaryTap event. A sample and code snippet has been provided for your reference. Please review it for further details.


Code snippet :

        onCellSecondaryTap: (details) {

          int rowIndex = details.rowColumnIndex.rowIndex - 1;

          int colIndex = details.rowColumnIndex.columnIndex;

          if (rowIndex >= 0 && colIndex >= 0) {

            // Check if the right-clicked row is already selected, Remove selection.

            if (_dataGridController.selectedRows

                .contains(employeeDataSource._employeeData[rowIndex])) {

              selectedRows.remove(employeeDataSource._employeeData[rowIndex]);

              _dataGridController.selectedRows = List.from(selectedRows);

              // Moves the current cell to the tapped cell when the navigation mode is set to 'cell'.

              _dataGridController

                  .moveCurrentCellTo(RowColumnIndex(rowIndex, colIndex));

            } else {

              // If not selected, select the row.

              _dataGridController.selectedRows = [];

              selectedRows.add(employeeDataSource._employeeData[rowIndex]);

              _dataGridController.selectedRows = List.from(selectedRows);

              // Moves the current cell to the tapped cell when the navigation mode is set to 'cell'.

              _dataGridController

                  .moveCurrentCellTo(RowColumnIndex(rowIndex, colIndex));

            }

          }

        },


Regards,

Abinesh P 


Attachment: SfDataGrid_f2493787.zip

Marked as answer
Loader.
Up arrow icon