回答
您可以使用自定義單元格繪製標題來獲得想要的結果。
在回答您的意見,要求的方式來對準電池底部的文字,我已經添加評論我的代碼。他們希望清楚。
你需要下面的代碼(比如在Form_Load初始化組件後)
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);
接下來,你需要像下面的代碼:
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 < titleSize.Width)
{
this.dataGridView1.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;
}
}
DataGrid d = new DataGrid();
d.Columns[0].HeaderStyle.VerticalAlign = VerticalAlign.Bottom;
如果你正在尋找的GridView那麼你區分這樣的: -
GridView gv = new GridView();
gv.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Bottom;
如果你正在尋找的datagridview那麼情況是這樣的: - 創建的對象datagridview的。
gv.Columns["ColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;
@Diego - 得到它.... – TechGiant 2011-04-25 20:14:29
@TechGiant:對於一個DataGridView第二行拋出2個錯誤: 「System.Windows.Forms的。的DataGridViewColumn」不包含用於定義‘ItemStyle’和沒有擴展方法‘ItemStyle’接受型的第一參數‘System.Windows.Forms.DataGridViewColumn’可以找到(是否缺少using指令或程序集引用) 和:名稱'VerticalAlign'在當前上下文中不存在 – Diego 2011-04-25 20:24:33
@Diego - 對於數據網格查看我寫了最後一行。嘗試的最後一行,這將使你的結果 – TechGiant 2011-04-25 20:25:50
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.
一種更簡單,更有效的渲染器
無論是通過設計附加事件,或與這行代碼
dataGridView1.CellPainting += new DataGridView1_CellPainting(dataGridView1_CellPainting);
事件處理程序繪製旋轉的文本
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
// Vertical text from column 0, or adjust below, if first column(s) to be skipped
if (e.RowIndex == -1 && e.ColumnIndex >= 0) {
e.PaintBackground(e.CellBounds, true);
e.Graphics.TranslateTransform(e.CellBounds.Left , e.CellBounds.Bottom);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(e.FormattedValue.ToString(),e.CellStyle.Font,Brushes.Black,5,5);
e.Graphics.ResetTransform();
e.Handled = true;
}
}
- 1. 垂直文本旁邊的垂直線
- 2. 垂直對齊SlickGrid頭垂直文本
- 3. ImageMagick:垂直居中文本?
- 4. 垂直居中文本
- 5. CSS垂直居中文本
- 6. 垂直居中文本
- 7. Datagridview水平垂直按鈕
- 8. 帶CSS的垂直文本
- 9. 標籤中的垂直居中文本
- 10. 在div中垂直居中的文本
- 11. 垂直快捷菜單欄文本不會垂直居中
- 12. 中心垂直文本文件輸入
- 13. 垂直對齊標籤中的文本
- 14. WPF:DatePicker的文本垂直居中
- 15. WPF TextBlock中的文本垂直對齊
- 16. 垂直對齊NSTableView行中的文本
- 17. 如何使html中的文本垂直?
- 18. 垂直軸中間的Java JLabel文本
- 19. css中的垂直文本高度
- 20. dijit中的垂直文本TabContainer標籤
- 21. d3中的垂直文本(不旋轉)
- 22. 多格與垂直文本
- 23. 抵銷文本垂直
- 24. jQuery垂直文本滾動
- 25. 繪製垂直文本()
- 26. 垂直文本與jQuery
- 27. UITextView文本垂直對齊
- 28. Primefaces列垂直文本
- 29. 垂直排列文本
- 30. CSS:垂直對齊文本?
雖然,我通常要更改datagridview的標準行爲的警告 - 做檢查,如果這是必須的。控制可以做了很多事情,但經常會有意想不到的副作用,可能會使他們的腦袋落在軌道上。 – 2011-04-25 20:54:26
謝謝!!您現在如何更改此代碼以使文本在單元格底部對齊? – Diego 2011-04-26 10:29:21
@Diego我已經添加了與單元格底部對齊的代碼,儘管如此,請徹底測試它,因爲它使用單元格繪畫爲標題觸發兩次的事實,這可能並非總是如此。 – 2011-04-26 22:10:02