Graphics.MeasureString too short

R

Rainer Queck

Hello NG,

I am trying to adjust the column width of a DataGridView to the Text in its
column header.
To do this I am using the Graphics.MeasureString method. Unfortunatly I
alway get a too short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

Regards
Rainer Queck
 
M

Morten Wennevik [C# MVP]

Rainer Queck said:
Hello NG,

I am trying to adjust the column width of a DataGridView to the Text in its
column header.
To do this I am using the Graphics.MeasureString method. Unfortunatly I
alway get a too short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

Regards
Rainer Queck

Hi Rainer,

Graphics.MeasureString is a quick way to measure a string, but the resulting
size is only an approximate measure. Use Graphics.MeasureCharacterRanges()
or TextRenderer.MeasureText(). PS! I believe MeasureText pads the size by
default so use TextFormatFlags.NoPadding if this is an issue.
MeasureCharacterRanges is the most complex of the three measuring solutions,
but may give the best result.
 
R

Rainer Queck

Hello Morten,

thanks for your advices. I will give it a try.

Regards
Rainer
 
R

Rainer Queck

Hello Morten,

unfortunately you suggestions did not work. I still endup in having even
shorter "widths".
Currently I get the best result with Graphics.MeasureString and handing
over a string which is enlarged by "__" (two underscore).

This is a workaround and I would like to know how to do it right.

Regards
Rainer
 
B

Ben Voigt [C++ MVP]

Rainer said:
Hello Morten,

unfortunately you suggestions did not work. I still endup in having
even shorter "widths".
Currently I get the best result with Graphics.MeasureString and
handing over a string which is enlarged by "__" (two underscore).

This is a workaround and I would like to know how to do it right.

You are setting the column width, but there is some margin so that the
columns don't run together, as a result the column content is allowed less
space than the column width. You'll have to add that margin to the content
width to get the required column width.
 
R

Rune Huseby

Hello NG,

I am trying to adjust the column width of a DataGridView to the Text
in its column header.
To do this I am using the Graphics.MeasureString method. Unfortunatly
I alway get a too short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

You need to have space for the gridlines to, and some margin.

This is how the text-box-columns calculate its preferred size:
protected internal override Size GetPreferredSize(Graphics g, object value)
{
Size extents = Size.Ceiling(g.MeasureString(GetText(value),
DataGridTableStyle.DataGrid.Font));
extents.Width += xMargin*2 + this.DataGridTableStyle.GridLineWidth;
extents.Height += yMargin;
return extents;
}


hint: The .NET source-code can be downloaded using this tool:
http://www.codeplex.com/NetMassDownloader
 
M

Michael F. Sargent

MeasureString has some usable overloads that may help you. For example, I use:

SizeF tabSize = graphics.MeasureString( "M", font, rectfText.Size, StringFormat.GenericTypographic );

This overload is described in http://msdn.microsoft.com/en-us/library/ms142109.aspx
The note under Remarks is of interest:

The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the
string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might
display a string narrower than reported by MeasureString. To obtain metrics suitable for adjacent strings in layout (for example,
when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a
StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.

Hope this helps,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top