HighTechTalks DotNet Forums  

Datagridview header cells with vertical text

Dotnet Framework (WinForms DesignTime) microsoft.public.dotnet.framework.windowsforms.designtime


Discuss Datagridview header cells with vertical text in the Dotnet Framework (WinForms DesignTime) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Roger Tranchez
 
Posts: n/a

Default Datagridview header cells with vertical text - 09-12-2007 , 11:22 AM







Hello,

I have a datagridview with lots of columns selectable with a checkbox
control, and I would like to have its header text as narrow as a check box,
but for this I have to display its text vertically...

How can I achieve this ?

Very thanks in advance !
--
Roger Tranchez
MCTS
..NET 2005 and DB developer

Reply With Quote
  #2  
Old   
Linda Liu [MSFT]
 
Posts: n/a

Default RE: Datagridview header cells with vertical text - 09-12-2007 , 11:41 PM






Hi Roger,

Sorry that I may not understand your question exactly. Could you please
explain more about your question?


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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


Reply With Quote
  #3  
Old   
Roger Tranchez
 
Posts: n/a

Default RE: Datagridview header cells with vertical text - 09-13-2007 , 06:32 AM



Hello,

Here's an example:

You can see below a datagridview with two columns with their header text
"text1" and "text2". The values for these columns are boolean, so I use the
checkbox to introduce the values (represented by Xs) as follows:

t t
e e
x x
t t
1 2 ...
--------------------------------------------------------
X X

As you can see, the header text are "vertically oriented" because I want to
make the best use of the form's space (I will have LOTS of checkbox columns)
.... and I don't know how to achieve this "vertical effect".

I hope you can understand what I mean.

Thanks,


--
Roger Tranchez
MCTS
..NET 2005 and DB developer


"Linda Liu [MSFT]" wrote:

Quote:
Hi Roger,

Sorry that I may not understand your question exactly. Could you please
explain more about your question?


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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



Reply With Quote
  #4  
Old   
Roger Tranchez
 
Posts: n/a

Default RE: Datagridview header cells with vertical text - 09-13-2007 , 10:46 AM



I'm sorry but I have to make a correction:

the header text must go from bottom to top:

imagine you have a book in front of you, and you rotate it 90 degrees
counter-clockwise. This is the orientation I want for the text in the headers
(you will read it from bottom to top, letters will be 90 degrees-rotated)

Thanks,

Roger


--
Roger Tranchez
MCTS
..NET 2005 and DB developer


"Roger Tranchez" wrote:

Quote:
Hello,

Here's an example:

You can see below a datagridview with two columns with their header text
"text1" and "text2". The values for these columns are boolean, so I use the
checkbox to introduce the values (represented by Xs) as follows:

t t
e e
x x
t t
1 2 ...
--------------------------------------------------------
X X

As you can see, the header text are "vertically oriented" because I want to
make the best use of the form's space (I will have LOTS of checkbox columns)
... and I don't know how to achieve this "vertical effect".

I hope you can understand what I mean.

Thanks,


--
Roger Tranchez
MCTS
.NET 2005 and DB developer


"Linda Liu [MSFT]" wrote:

Hi Roger,

Sorry that I may not understand your question exactly. Could you please
explain more about your question?


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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



Reply With Quote
  #5  
Old   
Linda Liu [MSFT]
 
Posts: n/a

Default RE: Datagridview header cells with vertical text - 09-14-2007 , 05:04 AM



Hi Roger,

Thank you for your reply and detailed explanation! I can understand your
question now.

Firstly, to custom draw a DataGridView, we need to handle its CellPainting
event.

Secondly, to draw a vertical oriented text, we could call the
Graphics.DrawString(string,Brush,RectangleF,String Format) method passing an
instance of StringFormat with a DirectionVertical format flag to this
method. For example:

System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// g is a Graphics object. The text "hello" will be drawn in vertical
orientation
g.DrawString("hello", this.Font, Brushes.Orange, new PointF(0,
0),drawFormat);

Unfortunately, the reading direction of the text "hello" would be from top
to bottom using the above method, which doesn't meet your requirement.

A solution is to call the Graphics.TranslateTransform and RotateTransform
methods to get what you want. The following is a sample.

void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex >= 0)
{
e.PaintBackground(e.ClipBounds, true);
Rectangle rect =
this.dataGridView1.GetColumnDisplayRectangle(e.Col umnIndex, true);
Size titleSize =
TextRenderer.MeasureText(e.Value.ToString(), e.CellStyle.Font);
if (this.dataGridView1.ColumnHeadersHeight <
titleSize.Width)
this.dataGridView1.ColumnHeadersHeight =
titleSize.Width;

e.Graphics.TranslateTransform(0, titleSize.Width);
e.Graphics.RotateTransform(-90.0F);

e.Graphics.DrawString(e.Value.ToString(), this.Font,
Brushes.Orange, new PointF(rect.Y, rect.X));

e.Graphics.RotateTransform(90.0F);
e.Graphics.TranslateTransform(0, -titleSize.Width);
e.Handled = true;
}
}

In addition, you could set the AutoSizeColumnsMode property of the
DataGridView to AllCellsExceptHeader in order to make the DataGridView
compact.

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

Sincerely,
Linda Liu
Microsoft Online Community Support


Reply With Quote
  #6  
Old   
Roger Tranchez
 
Posts: n/a

Default RE: Datagridview header cells with vertical text - 09-14-2007 , 05:26 AM



Hello,

Really thanks for your answer; old Boss Bill can be proud of you 8-D.

I'll implement it this way.

--
Roger Tranchez
MCTS
..NET 2005 and DB developer


"Linda Liu [MSFT]" wrote:

Quote:
Hi Roger,

Thank you for your reply and detailed explanation! I can understand your
question now.

Firstly, to custom draw a DataGridView, we need to handle its CellPainting
event.

Secondly, to draw a vertical oriented text, we could call the
Graphics.DrawString(string,Brush,RectangleF,String Format) method passing an
instance of StringFormat with a DirectionVertical format flag to this
method. For example:

System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// g is a Graphics object. The text "hello" will be drawn in vertical
orientation
g.DrawString("hello", this.Font, Brushes.Orange, new PointF(0,
0),drawFormat);

Unfortunately, the reading direction of the text "hello" would be from top
to bottom using the above method, which doesn't meet your requirement.

A solution is to call the Graphics.TranslateTransform and RotateTransform
methods to get what you want. The following is a sample.

void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex >= 0)
{
e.PaintBackground(e.ClipBounds, true);
Rectangle rect =
this.dataGridView1.GetColumnDisplayRectangle(e.Col umnIndex, true);
Size titleSize =
TextRenderer.MeasureText(e.Value.ToString(), e.CellStyle.Font);
if (this.dataGridView1.ColumnHeadersHeight
titleSize.Width)
this.dataGridView1.ColumnHeadersHeight =
titleSize.Width;

e.Graphics.TranslateTransform(0, titleSize.Width);
e.Graphics.RotateTransform(-90.0F);

e.Graphics.DrawString(e.Value.ToString(), this.Font,
Brushes.Orange, new PointF(rect.Y, rect.X));

e.Graphics.RotateTransform(90.0F);
e.Graphics.TranslateTransform(0, -titleSize.Width);
e.Handled = true;
}
}

In addition, you could set the AutoSizeColumnsMode property of the
DataGridView to AllCellsExceptHeader in order to make the DataGridView
compact.

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

Sincerely,
Linda Liu
Microsoft Online Community Support



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.