AutoResizeColumns in a DataGridView

  • Thread starter Thread starter Jörgen Ahrens
  • Start date Start date
J

Jörgen Ahrens

Hi All

VS 2005 SP1

I use a DataGridView to display some figures. I have a cellpainting event
that displays a couple of numbers in bold and other numbers in regular font
style. When i double click the Column devider it resizes the column so that
the regular formated columns fit in but not the bold formated columns.
What is te best workaround for this problem?

I tryed overwritting the ColumnDividerDoubleClick event:
private void gridEditColumn_ColumnDividerDoubleClick(object sender,
DataGridViewColumnDividerDoubleClickEventArgs e)
{
Font font = new Font(gridEditColumn.DefaultCellStyle.Font,
FontStyle.Bold);
Graphics g = gridEditColumn.CreateGraphics();
Size s;

int width = 0;

for (int i = 0; i < gridEditColumn.Rows.Count; i++)
{
s =
g.MeasureString(gridEditColumn.Rows.Cells[e.ColumnIndex].FormattedValue.ToString(),
font).ToSize();

if (width < s.Width)
{
width = s.Width;
}
}

gridEditColumn.Columns[e.ColumnIndex].Width = width;

e.Handled = true;
}

but the width i get is still to small so that the bold value doesn't fit
in... i'm scared to just add a couple of pixels to the width...or do i have
to do so?
is there a better way to do it?

thanks for your help.
jahrens
 
Hi Jahrens,

I peformed a test based your sample code and did reproduce the problem on
my side.

All your sample code are correct, except one place where you get the
caculated size returned from the Graphics.MeasureString method.

The Graphics.MeasureString method returns a value of type SizeF. In your
sample code, you call the ToSize method of SizeF to get a size composed of
a width and a height of type int. This would discard the decimal fraction
of the SizeF value.

I modify your sample code as follows and it turns out to work on my side.

SizeF s;
int width = 0;

for (int i = 0; i < gridEditColumn.Rows.Count; i++)
{
s =
g.MeasureString(gridEditColumn.Rows.Cells[e.ColumnIndex].FormattedValue.T
oString(), font);

if (width < s.Width)
{
width = (int)(s.Width + 1);
}
}

Please try my suggestion and let me know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks for your response.



+ 1 does not do the trick



It needs to be at least + 15 to fit inside...

I don't get it...why is that?

Has onyone a solution?



Thanks

jahrens
 
Hi Jahrens,

Thank you for your feedback.

Could you please show me the code in your CellPainting event handler? I'd
like to see how you paint the cell value in the DataGridViewCell.

I searched in our inner database and found a document saying that
Graphics.MeasureString is not accurate with Graphics.DrawString, because
the text is rendered with hinted widths, but measured with nominal widths.
It also says this behavior is by design.

You may have a try using TextRenderer.MeasureText method to caculate the
needed width.

Hope this helps.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Thanks for your help

here is my cellPainting stuff:

e.CellStyle.ForeColor = Color.Blue;
e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);

The TextRender.MeasureText() instead of the g.measureString() does the
trick!
or is there a way to set the CellStyle diffrent so i don't have to code my
own ColumnDividerDoubleClick?

Thanks
jahrens
 
Hi Jahrens,

Thank you for your prompt response.

Glad to hear that the problem is fixed using TextRenderer.MeasureText
method.

It seems that you paint the cell value using the statement
'e.PaintContent(e.ClipBounds)'. Am I right? I used the statement
'e.Graphics.DrawString(e.Value.ToString(), font,Brushes.Blue,e.CellBounds)'
in my test.

I noticed that the text is drawn differently using these two methods. The
text drawn with the former method is wider than with the latter method.
That's why the results of using Graphics.MeasureString in my test is
different from that in your program.

If the all the cells in a DataGridViewColumn would draw their values with
the same font, you may set the DefaultCellStyle.Font property of the
DataGridViewColumn to that font. When the program is run and you
double-click the column devider, the column will resize to a proper width
to contain all the content in the column automatically, i.e. you needn't
handle the ColumnDividerDoubleClick event of the DataGridViewColumn.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi Jahrens,

How about the problem now?

If you have any other question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top