Text Extent - Resize DataGridView header cell's height

A

Ashutosh

Hi,
How can I find out the width in pixel that a particular text requires to
be drawn/written on a graphics object. (Similar to VC++ CDC::GetTextExtent)

I am displaying some data in DataGridView and the column headers are
vertical. So, I need to resize the header cell's height based on the max
space required to display the longest header text. This must be done at
runtime as there is no information of the data that will be displayed in
the grid.

I am using the following text to change the orientation of the displayed
text.

private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex >= 0)
{
e.PaintBackground(e.CellBounds, true);
e.Graphics.TranslateTransform(e.CellBounds.Left,
e.CellBounds.Bottom);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(e.FormattedValue.ToString(),
e.CellStyle.Font, Brushes.Black, 5, 5);
e.Graphics.ResetTransform();
e.Handled = true;
}
}


Thanks & Regards,
Ashutosh
 
Z

Zhi-Xin Ye [MSFT]

Hello Ashutosh ,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

You can call the Graphics.MeasureString() method to measure the size of the
column header text.

For example:

private void Form1_Load(object sender, EventArgs e)
{
//....... other code here.....

this.dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
float max = 0;
using (Graphics g = this.dataGridView1.CreateGraphics())
{
for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
{
float w =
g.MeasureString(this.dataGridView1.Columns[j].HeaderText,

this.dataGridView1.ColumnHeadersDefaultCellStyle.Font).Width;

if (w > max)
{
max = w;
}
}
}

//Since you draw the header text at point(5,5), add 10 pixels
as padding.
this.dataGridView1.ColumnHeadersHeight = (int)max + 10;
}


For more information about the Graphics.MeasureString() method, you can
refer to this document:

Graphics.MeasureString() method
http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx

If you have any questions or concerns, please feel free to let me know.

Have a happy day!

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel

free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2

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. 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/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Ashutosh

Thanks a lot....I did lookup all the methods in Graphics class and font
class....don't know how I missed the MeasureString method...may be I was
sleeping :)

Zhi-Xin Ye said:
Hello Ashutosh ,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

You can call the Graphics.MeasureString() method to measure the size of the
column header text.

For example:

private void Form1_Load(object sender, EventArgs e)
{
//....... other code here.....

this.dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
float max = 0;
using (Graphics g = this.dataGridView1.CreateGraphics())
{
for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
{
float w =
g.MeasureString(this.dataGridView1.Columns[j].HeaderText,

this.dataGridView1.ColumnHeadersDefaultCellStyle.Font).Width;

if (w > max)
{
max = w;
}
}
}

//Since you draw the header text at point(5,5), add 10 pixels
as padding.
this.dataGridView1.ColumnHeadersHeight = (int)max + 10;
}


For more information about the Graphics.MeasureString() method, you can
refer to this document:

Graphics.MeasureString() method
http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx

If you have any questions or concerns, please feel free to let me know.

Have a happy day!

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel

free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2

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. 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/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Z

Zhi-Xin Ye [MSFT]

Hi Ashutosh,

I'm glad to hear that my suggestion helps you. Have a happy day!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel

free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Top