Articles in this section
Category / Section

How to get the record using mouse double click in the row in WPF DataGrid (SfDataGrid)?

2 mins read

You can get the current record from the resolved record index using the ResolveToRecordIndex method in WPF DataGrid (SfDataGrid). The record index in the clicked position can be retrieved by resolving the mouse click position through VisualContainer.PointToCellRowColumnIndex. (e.g.  MouseDoubleClick event for WPF and OnDoubleTapped event for UWP).

 

Refer to the code to get the record of a row when double clicking the row in SfDataGrid.

 

void AssociatedObject_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var visualcontainer = this.AssociatedObject.GetVisualContainer();
    // get the row and column index based on the pointer position in WPF
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(e.GetPosition(visualcontainer));
    if (rowColumnIndex.IsEmpty)
        return;
    var colindex = this.AssociatedObject.ResolveToGridVisibleColumnIndex(rowColumnIndex.ColumnIndex);
    if (colindex < 0 || colindex >= this.AssociatedObject.Columns.Count)
        return;
 
    var recordindex = this.AssociatedObject.ResolveToRecordIndex(rowColumnIndex.RowIndex);
    if (recordindex < 0)
        return;
 
    var recordentry = this.AssociatedObject.View.GroupDescriptions.Count == 0 ? this.AssociatedObject.View.Records[recordindex] : this.AssociatedObject.View.TopLevelGroup.DisplayElements[recordindex];
 
    //Returns if caption summary or group summary row encountered.
    if (!recordentry.IsRecords)
        return;
 
    object value = null;
 
    if (!this.AssociatedObject.IsInDetailsViewIndex(rowColumnIndex.RowIndex))
    {               
        var record = (recordentry as RecordEntry).Data;
        value = record.GetType().GetProperty(AssociatedObject.Columns[colindex].MappingName).GetValue(record) ?? string.Empty;
        MessageBox.Show("Cell Value : " + value.ToString());
    }
}

 

 

Refer to the code to get the record of a row when double clicking the row in DeatilsViewDataGrid.

 

void detailsViewDataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{           
    var detailsViewDataGrid = sender as DetailsViewDataGrid;
    //Here you can get the DetailsViewDataGridVisualContainer.
    var detailsViewVisualContainer = detailsViewDataGrid.GetVisualContainer();
    //Here you can get the rowColumnIndex based on position of DetailsViewDataGridVisualContainer.
    var detailsViewRowColumnIndex = detailsViewVisualContainer.PointToCellRowColumnIndex(e.GetPosition(detailsViewVisualContainer), true);
    var detailsViewrecordIndex = detailsViewDataGrid.ResolveToRecordIndex(detailsViewRowColumnIndex.RowIndex);
    var detailsViewRecordEntry = detailsViewDataGrid.View.GroupDescriptions.Count == 0 ? detailsViewDataGrid.View.Records[detailsViewrecordIndex] : detailsViewDataGrid.View.TopLevelGroup.DisplayElements[detailsViewrecordIndex];
    //Returns if caption summary or group summary row encountered.
    if (!detailsViewRecordEntry.IsRecords)
        return;
    var record = (detailsViewRecordEntry as RecordEntry).Data;
   
    var value = record.GetType().GetProperty(detailsViewDataGrid.Columns[detailsViewRowColumnIndex.ColumnIndex].MappingName).GetValue(record) ?? string.Empty;
    MessageBox.Show("Cell Value : " + value.ToString());
}

Refer to the screenshot to get the record when using MouseDoubleClick event in SfDataGrid.

Get the record data in WPF DataGrid

 

Refer to the screenshot to get the record when using the MouseDoubleClick event in DetailsViewDataGrid.

Get the record while mouse double click on WPF DataGrid

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied