Articles in this section
Category / Section

How to show the tooltip for specified cell in WPF DataGrid (SfDataGrid)?

2 mins read

You can display the information of a specified GridCell as tooltip in WPF DataGrid (SfDataGrid) by getting the cell information from the cell index resolved from the mouse hover position by VisualContainer. PointToCellRowColumnIndex.

In WPF sample, the tooltip for a specified GridCell has been shown in the SfDataGrid.MouseMove event.

Refer to the code examples to show the cell information as a ToolTip.

SfDataGridBehavior.cs

Public class SfDataGridBehavior : Behavior<SfDataGrid>
{
    SfDataGrid dataGrid = null;
    ToolTip toolTip = new ToolTip();
 
    protected override void OnAttached()
    {
        dataGrid = this.AssociatedObject as SfDataGrid;
        dataGrid.MouseMove += DataGrid_MouseMove;
    }
 
    private void DataGrid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
        var visualContainer = dataGrid.GetVisualContainer();
        var point = e.GetPosition(visualContainer);
        var rowcolumnindex = visualContainer.PointToCellRowColumnIndex(point);
        var rowindex = rowcolumnindex.RowIndex;
        var columnindex = rowcolumnindex.ColumnIndex;
        // Get the resolved current record index
        var recordIndex = this.dataGrid.ResolveToRecordIndex(rowindex);
 
        if (rowindex == 2 && columnindex == 1) 
        {
            if (toolTip.IsOpen)
                return;
            // Get the current row record
            var mappingName = this.dataGrid.Columns[columnindex].MappingName;
            var record = this.dataGrid.View.Records.GetItemAt(recordIndex);
            var cellvalue = record.GetType().GetProperty(mappingName).GetValue(record, null).ToString();
            toolTip.Content = cellvalue;
            toolTip.IsOpen = true;
            toolTip.StaysOpen = true;
        }
        toolTip.IsOpen = false;
        toolTip.StaysOpen = false;
    }
}

In UWP sample, the tooltip for a specified GridCell has been shown in the SfDataGrid.PointerMoved event.

SfDataGridBehavior.cs

Public class SfDataGridBehavior :Behavior<SfDataGrid>
{
    SfDataGrid dataGrid = null;
    ToolTip toolTip = new ToolTip();
    RowColumnIndex rowColumnIndex = new RowColumnIndex();
 
    protected override void OnAttached()
    {
        dataGrid = this.AssociatedObject as SfDataGrid;
        dataGrid.PointerMoved += DataGrid_PointerMoved;
    }
 
    private void DataGrid_PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        var visualContainer = dataGrid.GetVisualContainer();
        var point = e.GetCurrentPoint(visualContainer).Position;
        var rowcolumnindex = visualContainer.PointToCellRowColumnIndex(point);
        var rowindex = rowcolumnindex.RowIndex;
        if(rowindex != rowColumnIndex.RowIndex)
            toolTip.IsOpen = false;
        var columnindex = rowcolumnindex.ColumnIndex;
        rowColumnIndex = rowcolumnindex;
        // Get the resolved current record index
        var recordIndex = this.dataGrid.ResolveToRecordIndex(rowindex);
 
        if (rowindex == 2 && columnindex == 1) // EmployeeName
        {
            // Get the current row record
            var mappingName = this.dataGrid.Columns[columnindex].MappingName;
            var record = this.dataGrid.View.Records.GetItemAt(recordIndex);
            var cellvalue = record.GetType().GetProperty(mappingName).GetValue(record, null).ToString();
            toolTip.Content = cellvalue;
            var dataColumnBase = SelectionHelper.GetDataColumnBase(this.dataGrid, new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex(rowindex,columnindex));
            if (dataColumnBase != null)
            {
                GridCell gridCell = dataColumnBase.ColumnElement as GridCell;
                if (gridCell != null)
                {
                    ToolTipService.SetToolTip(gridCell, toolTip);
                    toolTip.IsOpen = true;
                }
            }
        }
    }
}

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