We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Get cell text rectangle width

Hi there,

If I have a cell which has a small button embedded on the right, how do I obtain the cell text rectangle width? (the width of the column that can accommodate the text).

Obviously Syncfusion.Windows.Forms.Grid.GridControlBase.GetColWidth(int colIndex) returns the entire column width (which includes the button width as well).

Reason for asking is I need to display a text extender on mouse hover when the text is trimmed (by setting the GridStyleInfo.Trimming).
The only way I know the text has been trimmed is by comparing if (text_length > cell_text_rectangle_width).

Thank you for any help :)

1 Reply

RC Rajadurai C Syncfusion Team October 19, 2009 02:41 PM UTC

Hi Magdalena,

Thanks for your interest in Syncfusion Products.

You can check for the cellmodel and get the button width and subtract it from the width calculated, to achieve the expected result. Here is some code for your reference handled in MouseMove event of gridcontrol to show the tooltip for the partially visible text in cell.

int row1, col1 = -1;
int colWidth;
void gridControl1_MouseMove(object sender, MouseEventArgs e)
{
int row, col;
if (this.gridControl1.PointToRowCol(new Point(e.X, e.Y), out row, out col) && (col != col1 || row != row1))
{
col1 = col;
row1 = row;
if (this.toolTip1 != null && this.toolTip1.Active)
this.toolTip1.Active = false;
Graphics g = CreateGraphics();
GridStyleInfo style = this.gridControl1[row, col];
if(style.CellType == "ComboBox")
colWidth = this.gridControl1.ColWidths[col] - style.TextMargins.Left - style.TextMargins.Right - style.CellModel.ButtonBarSize.Width;
else
colWidth = this.gridControl1.ColWidths[col] - style.TextMargins.Left - style.TextMargins.Right;
if (colWidth < g.MeasureString(style.Text, style.GdipFont).Width)
{
toolTip1.SetToolTip(this.gridControl1, this.gridControl1[row1, col1].Text);
toolTip1.Active = true;
}
g.Dispose();
}
}

Here is a sample attached for your reference in which this code has been implemented.
http://files.syncfusion.com/support/samples/Grid.Windows/7.3.0.20/F90766.zip

Regards,
Rajadurai

Loader.
Up arrow icon