RE: Datagridview header cells with vertical text
- From: Roger Tranchez <run1789@xxxxxxxxxxxxxxxx>
- Date: Fri, 14 Sep 2007 02:26:02 -0700
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:
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,StringFormat) 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.ColumnIndex, 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
- References:
- RE: Datagridview header cells with vertical text
- From: Linda Liu [MSFT]
- RE: Datagridview header cells with vertical text
- From: Roger Tranchez
- RE: Datagridview header cells with vertical text
- From: Roger Tranchez
- RE: Datagridview header cells with vertical text
- From: Linda Liu [MSFT]
- RE: Datagridview header cells with vertical text
- Prev by Date: RE: Datagridview header cells with vertical text
- Next by Date: UserControl Public property and Designer Code
- Previous by thread: RE: Datagridview header cells with vertical text
- Next by thread: UserControl Public property and Designer Code
- Index(es):
Relevant Pages
|
Loading