2011-04-25 61 views

回答

15

您可以使用自定義單元格繪製標題來獲得想要的結果。

在回答您的意見,要求的方式來對準電池底部的文字,我已經添加評論我的代碼。他們希望清楚。

你需要下面的代碼(比如在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; 
    } 
} 
+0

雖然,我通常要更改datagridview的標準行爲的警告 - 做檢查,如果這是必須的。控制可以做了很多事情,但經常會有意想不到的副作用,可能會使他們的腦袋落在軌道上。 – 2011-04-25 20:54:26

+0

謝謝!!您現在如何更改此代碼以使文本在單元格底部對齊? – Diego 2011-04-26 10:29:21

+1

@Diego我已經添加了與單元格底部對齊的代碼,儘管如此,請徹底測試它,因爲它使用單元格繪畫爲標題觸發兩次的事實,這可能並非總是如此。 – 2011-04-26 22:10:02

-1
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; 
+0

@Diego - 得到它.... – TechGiant 2011-04-25 20:14:29

+0

@TechGiant:對於一個DataGridView第二行拋出2個錯誤: 「System.Windows.Forms的。的DataGridViewColumn」不包含用於定義‘ItemStyle’和沒有擴展方法‘ItemStyle’接受型的第一參數‘System.Windows.Forms.DataGridViewColumn’可以找到(是否缺少using指令或程序集引用) 和:名稱'VerticalAlign'在當前上下文中不存在 – Diego 2011-04-25 20:24:33

+0

@Diego - 對於數據網格查看我寫了最後一行。嘗試的最後一行,這將使你的結果 – TechGiant 2011-04-25 20:25:50

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

一種更簡單,更有效的渲染器

無論是通過設計附加事件,或與這行代碼

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; 
    } 
}