VerticalText in DataGrid
-
Monday, October 29, 2007 10:44 PM
Hello, how can I "rotate" text in Cell of DataGrid or similiar control, like in Excell cell ?
All Replies
-
Wednesday, October 31, 2007 3:36 PM
While there may be a third party control for this, out of the box you will have to use Graphics.DrawString to make this happen.
Could always wrap this in a custom control, then include that in a template field on your grid.
-
Wednesday, October 31, 2007 10:05 PM
Thx, but I know what to do, my problem is how -
Thursday, November 01, 2007 1:12 AM
GregBednarski wrote: Thx, but I know what to do, my problem is how
See the MSDN article on Graphics.Drawstring
http://msdn2.microsoft.com/en-us/library/system.drawing.graphics.drawstring(VS.71).aspx
Without your app in front of me (and some coding time) all I can do is point you in the right direction. The stuff is not something you just tweak and drop in. tbh, unless it's something absolutely critical to the app, I wouldn't bother (or if it is, seriously consider a third party control).
-
Thursday, November 01, 2007 10:32 AM
You can use the StringFormatFlags.DirectionVertical flag while handle the CellPainting event to draw the string with vertical direction, something like this:
Code Blockprivate void Form11_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("c1");
dt.Columns.Add("c2");
for (int j = 0; j < 10; j++)
{
dt.Rows.Add("aaaaaaaaa", "bbbb");
}
this.dataGridView2.DataSource = dt;
for (int j = 0; j < 10; j++)
{
int height = TextRenderer.MeasureText(
this.dataGridView2[0, j].Value.ToString(),
this.dataGridView2.DefaultCellStyle.Font).Width;
this.dataGridView2.Rows[j].Height = height;
}
this.dataGridView2.CellPainting += new
DataGridViewCellPaintingEventHandler(dataGridView2_CellPainting); ;}
void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.Value != null)
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All
&~DataGridViewPaintParts.ContentForeground);StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.DirectionVertical;
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
new SolidBrush(e.CellStyle.ForeColor), e.CellBounds,sf);
e.Handled = true;
}
}
-
Friday, November 02, 2007 12:47 AM
Very funny Zhi-Xin Ye - MSFT
, it was 2 millenium ago when I wrote my last C routine, you spent a lot of time to help me, very thx, but I don't understand enything, could you translate your code into VB, please ? It will be very helpful in my future works.
You'll be my God when you do it !
-
Friday, November 02, 2007 2:10 AMVB.NET sample:
Code BlockPublic Class Form7
Private Sub Form7_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable
dt.Columns.Add("c1")
dt.Columns.Add("c2")
Dim j As Integer = 0
Do While (j < 10)
dt.Rows.Add("aaaaaaaaa", "bbbb")
j = (j + 1)
Loop
Me.DataGridView1.DataSource = dt
j = 0
Do While (j < 10)
Dim height As Integer = TextRenderer.MeasureText( _
Me.DataGridView1(0, j).Value.ToString, _
Me.DataGridView1.DefaultCellStyle.Font).Width
Me.DataGridView1.Rows(j).Height = height
j = (j + 1)
Loop
AddHandler DataGridView1.CellPainting, AddressOf Me.dataGridView1_CellPainting
End Sub
Private Sub dataGridView1_CellPainting(ByVal sender As Object, _
ByVal e As DataGridViewCellPaintingEventArgs)
If ((e.ColumnIndex = 0) _
AndAlso ((e.RowIndex > -1) _
AndAlso (Not (e.Value) Is Nothing))) Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not _
DataGridViewPaintParts.ContentForeground)
Dim sf As StringFormat = New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
sf.FormatFlags = StringFormatFlags.DirectionVertical
e.Graphics.DrawString(e.Value.ToString, e.CellStyle.Font, _
New SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf)
e.Handled = True
End If
End Sub
End Class


