New to Telerik UI for WinFormsStart a free 30-day trial

How to show tool tips when the cell's text doesn't fit

Updated over 6 months ago

Environment

Product VersionProductAuthor
2019.1.117RadGridView for WinFormsDesislava Yordanova

Description

RadGridView offers the ToolTipTextNeeded event which is suitable for assigning tool tips to its cell elements. A common requirement is not show the tool tip only if the cell's content doesn't fit the column's width.

show-tooltips-when-cell-text-does-not-fit

Solution

It is necessary to measure the height and width of the text and then depending on that to compare it with the cell dimensions. If the text exceeds the size of the cell, then you can show your tool tip text for the specified cell. You can easily achieve that behavior by using the ToolTipTextNeeded event. You should use TextPart object to measure text height and width.

ToolTipTextNeeded event handler

C#
        private void radGridView1_ToolTipTextNeeded(object sender, Telerik.WinControls.ToolTipTextNeededEventArgs e)
        {
            GridDataCellElement dataCell = sender as GridDataCellElement;
 
            if (dataCell != null)
            {
                TextPart textPart = new TextPart(dataCell);
                SizeF size = textPart.Measure(new SizeF(float.PositiveInfinity, float.PositiveInfinity));
                SizeF sizeInCell = textPart.Measure(new SizeF(dataCell.ColumnInfo.Width, float.PositiveInfinity));
 
                string toolTipText = null;
                float cellWidth = dataCell.ColumnInfo.Width;
                if (dataCell.MasterTemplate.ViewDefinition is HtmlViewDefinition)
                {
                    cellWidth = ((HtmlViewRowLayout)dataCell.TableElement.ViewElement.RowLayout).GetArrangeInfo(dataCell.ColumnInfo).Bounds.Width - dataCell.BorderWidth * 2;
                }
                float cellHeight = dataCell.Size.Height - dataCell.BorderWidth * 2;
 
                if (size.Width > cellWidth || cellHeight < sizeInCell.Height)
                {
                    toolTipText = dataCell.Text;
                }
 
                e.ToolTipText = toolTipText;
            }
        }
       

See Also