/// <summary> /// Show a tool tip containing the text of the cell at the specified coordinates, but only if the text /// doesn't fit into the cell. /// </summary> private string GetToolTipForTextColumn(int x, int y, DataGridCell hitCell, System.Windows.Forms.DataGridTextBoxColumn columnStyle) { string cellText = string.Empty; // Check whether the hit was within the text area of the cell, if possible. DataGridTextBoxColumn customTextColumn = columnStyle as DataGridTextBoxColumn; if (customTextColumn != null) { Rectangle textBounds = customTextColumn.GetTextBounds(hitCell); if (textBounds.Contains(x, y)) { cellText = GetTextForCell(hitCell, columnStyle); } } else { cellText = GetTextForCell(hitCell, columnStyle); } // Only display the tooltip if we have the text and it doesn't fit into the cell. if (cellText == null || (cellText.Length > 0 && DoesTextFitInCell(hitCell, cellText, columnStyle))) { cellText = string.Empty; } return(cellText); }
private bool DoesTextFitInCell(DataGridCell cell, string text, System.Windows.Forms.DataGridTextBoxColumn columnStyle) { Debug.Assert(text != null, "text != null"); // If the supplied column style is our own derived DataGridTextBoxColumn class ask it for the text // bounds, otherwise use the cell bounds. DataGridTextBoxColumn customColumnStyle = columnStyle as DataGridTextBoxColumn; Rectangle textBounds = (customColumnStyle == null ? GetCellBounds(cell) : customColumnStyle.GetTextBounds(cell)); SizeF size = new SizeF((float)textBounds.Width, (float)textBounds.Height); StringFormat format = new StringFormat(StringFormatFlags.NoWrap); int charsFitted; int linesFilled; CreateGraphics().MeasureString(text, Font, size, format, out charsFitted, out linesFilled); return(charsFitted == text.Length); }