NET Winforms: vertical text in datagridview?

You can achieve the result you want using custom cell painting for the header.

You can achieve the result you want using custom cell painting for the header. In answer to your comment asking for a way to align the text with the bottom of the cell, I've added comments to my code. They are hopefully clear.

You need the following code (say in the Form_Load after initializing components) dataGridView1. ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode. EnableResizing; dataGridView1.

ColumnHeadersHeight = 50; dataGridView1. AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode. AllCellsExceptHeader; // Here we attach an event handler to the cell painting event dataGridView1.

CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting); Next you need something like the following code: void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // check that we are in a header cell! 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 ColumnHeadersHeight = titleSize. Width; } e.Graphics. TranslateTransform(0, titleSize.

Width); e.Graphics. RotateTransform(-90.0F); // This is the key line for bottom alignment - we adjust the PointF based on the // ColumnHeadersHeight minus the current text width. ColumnHeadersHeight is the // maximum of all the columns since we paint cells twice - though this fact // may not be true in all usages!E.Graphics.

DrawString(e.Value.ToString(), this. Font, Brushes. Black, new PointF(rect.

Y - (dataGridView1. ColumnHeadersHeight - titleSize. Width) , rect.

X)); // The old line for comparison //e.Graphics. DrawString(e.Value.ToString(), this. Font, Brushes.

Black, new PointF(rect. Y, rect. X)); e.Graphics.

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

Handled = true; } }.

Although, my usual caveat with changing the standard behaviour of the datagridview - do check if this is a must have. The control can be made to do a lot of things but often there are unexpected side effects that can rear their heads down the track. – David Hall Apr 25 at 20:54 Thanks!

Do you now how to change this code to make the text be aligned at the bottom of the cell? – Diego Apr 26 at 10:29 1 @Diego I've added code which aligns to the bottom of the cell, though again, do test this thoroughly, since it uses the fact that cell painting fires twice for the headers, which might not always be true. – David Hall Apr 26 at 22:10 That worked great!

Thanks! – Diego Apr 27 at 18:27.

DataGrid d = new DataGrid(); d. Columns0.HeaderStyle. VerticalAlign = VerticalAlign.

Bottom; If you are looking for gridview then you case like this :- GridView gv = new GridView (); gv. Columns0.ItemStyle. VerticalAlign = VerticalAlign.

Bottom; If you are looking for datagridview then you case like this :- Create an object of datagridview. Gv. Columns"ColumnName".HeaderCell.Style.

Alignment = DataGridViewContentAlignment.BottomCenter.

Diego - got it.... – TechGiant Apr 25 at 20:14 @TechGiant: The second line for a DataGridView throws 2 errors: 'System.Windows.Forms. DataGridViewColumn' does not contain a definition for 'ItemStyle' and no extension method 'ItemStyle' accepting a first argument of type 'System.Windows.Forms. DataGridViewColumn' could be found (are you missing a using directive or an assembly reference) and: The name 'VerticalAlign' does not exist in the current context – Diego Apr 25 at 20:24 @Diego - for datagridview I have written last line .

Try the last line that will give your result – TechGiant Apr 25 at 20:25 @TechGiant: Also doen't compile: Non-invocable member 'System.Windows.Forms.DataGridView. Columns' cannot be used like a method. – Diego Apr 25 at 20:27 Wait for 2 min I am trying on my system... – TechGiant Apr 25 at 20:30.

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 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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions