将文本在DataGridView的单元格中竖直显示,需要在CellPainting事件中设置StringFormatFlag.DirectionVertical
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_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.dataGridView1.DataSource = dt;
this.dataGridView1.CellPainting += new
DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting); ;
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 1 && 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;
}
}
}
}