Selecting the current row during the PreviewMouseDown event

Basically, what it says on the title

I am looking to open a ContextMenu on the SfDataGrid during a long press, selecting the item/row as I do.

However, there seems to be no built-in way to access the row object during the event

I have tried traversing the VisualTree, but the Item is not a part of it.


2 Replies 1 reply marked as answer

MS Malini Selvarasu Syncfusion Team March 31, 2025 12:07 PM UTC

Hi Bill,
 

Query

Response

I am looking to open a ContextMenu on the SfDataGrid during a long press, selecting the item/row as I do.

 

Based on the information provided, we understand that you need to open the ContextMenu during a long press and selecting the item. We are currently analyzing the scenario and need some time to validate the issue. We will provide further details by April 2, 2025. We appreciate your patience in the meantime.

However, there seems to be no built-in way to access the row object during the event

I have tried traversing the VisualTree, but the Item is not a part of it.

 

Based on the information provided, we understand that you need to retrieve data during the `PreviewMouseDown` event. This can be achieved by obtaining the row and column index based on the pointer position using the `PointToCellRowColumnIndex()` method and then retrieving the data using these indices. 

 

Please refer to the code snippet below:
 

Code Snippet:
 

  private void SfDataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)

  {

 

      var visualcontainer = this.dataGrid.GetVisualContainer();

      // Gets the exact position where the mouse pointer is moved

      var point = e.GetPosition(visualcontainer);

      //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method

      var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);

      var recordIndex = this.dataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);

      if (recordIndex < 0)

          return;

      //When the rowindex is zero , the row will be header row

      if (!rowColumnIndex.IsEmpty)

      {

          if (this.dataGrid.View.TopLevelGroup != null)

          {

              // Get the current row record while grouping

              var record = this.dataGrid.View.TopLevelGroup.DisplayElements[recordIndex];

              if (record.GetType() == typeof(RecordEntry))

              {

                  var data = (record as RecordEntry).Data as OrderInfo;

              }

          }

 

          else

          {

              //For getting the record, need to resolve the corresponding record index from row index                     

              var record1 = this.dataGrid.View.Records[this.dataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex)].Data;

          }

          //Gets the column from ColumnsCollection by resolving the corresponding column index from  GridVisibleColumnIndex                      

          var gridColumn = this.dataGrid.Columns[this.dataGrid.ResolveToGridVisibleColumnIndex(rowColumnIndex.ColumnIndex)];

      }

 

 

  }


Regards,
Malini Selvarasu


Marked as answer

MS Malini Selvarasu Syncfusion Team April 2, 2025 01:32 PM UTC

Hi Bill,

Based on the information provided, we understand that you need to open the ContextMenu on a long press while selecting a row. To enable row selection, set AllowSelectionOnPointerPressed to true, ensuring the row is selected when pressed. To display the ContextMenu, handle the PreviewMouseDown event with a time delay.

To further assist you, we have prepared a sample demonstrating this approach. Please review the sample along with the accompanying code snippet, and let us know if this solution meets your requirements.
Code Snippet:
  private void SfDataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  {
      RowType rowType;
      var visualcontainer = this.dataGrid.GetVisualContainer();
      // Gets the exact position where the mouse pointer is moved
      var point = e.GetPosition(visualcontainer);
      var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
      rowType = visualcontainer.RowsGenerator.Items
.FirstOrDefault(item => item.Index == rowColumnIndex.RowIndex)?.RowType ?? RowType.DefaultRow;
      if (rowType==RowType.DefaultRow && e.ChangedButton == MouseButton.Right) // Detect Right-Click Long Press
      {
          // Start long-press timer
          _longPressTimer = new DispatcherTimer();
          _longPressTimer.Interval = TimeSpan.FromSeconds(1); // Adjust duration as needed
          _longPressTimer.Tick += LongPressTimer_Tick;
          _longPressTimer.Start();
      }
  }
 
  // Called when long press is detected
  private void LongPressTimer_Tick(object sender, EventArgs e)
  {
      // Check if the button is still pressed
      if (Mouse.RightButton == MouseButtonState.Pressed)
      {
          ShowContextMenu();
      }
      // Unsubscribe and stop the timer
      if (sender is DispatcherTimer timer)
      {
          timer.Tick -= LongPressTimer_Tick;// Unhook event
          timer.Stop(); // Stop the timer
      }
  }
 
  // Method to display ContextMenu
  private void ShowContextMenu()
  {
      dataGrid.RecordContextMenu.IsOpen = true;
      
  }

Thank you for your cooperation and understanding.
Regards,
Malini Selvarasu

Attachment: SfDataGrid_Demo_3338a2a6.zip

Loader.
Up arrow icon