2
我有一個DataGridView
我想設置一個標題行和列,如下圖所示: 如何設置標題爲DataGridView的行/列
文本1描述了列標題的含義和Text2的行。
我的問題是如果這是可能的,如果是這樣,如何做到這一點(覆蓋DataGridView
繪製事件?)或你可以提供什麼其他的替代方案來實現這個目標?我希望它是優雅和直觀的。
編輯:
我結束了使用下面的代碼:
private void dataGridView_tagretTable_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex == -1)
{
// Clear current drawing, to repaint when user change header width
e.Graphics.Clear(System.Drawing.Color.White);
string drawString = "Text1";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 8);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
// Measure string
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(drawString, drawFont);
// offset from rectangle borders
float offset = 3;
// Set string start point
float x = offset;
float y = e.Graphics.ClipBounds.Height - stringSize.Height - offset;
e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
drawString = "Text2";
// Measure string
stringSize = e.Graphics.MeasureString(drawString, drawFont);
// Set string start point
x = e.Graphics.ClipBounds.Width - stringSize.Width - offset;
y = offset;
e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
// Draw crossing line
Pen myPen = new Pen(Color.Black);
myPen.Width = 1;
e.Graphics.DrawLine(myPen, new Point(0, 0), new Point(e.ClipBounds.Width, e.ClipBounds.Height));
drawFont.Dispose();
drawBrush.Dispose();
drawFormat.Dispose();
myPen.Dispose();
// Set min row header width
if (dataGridView_tagretTable.RowHeadersWidth < 150)
{
dataGridView_tagretTable.RowHeadersWidth = 150;
}
e.Handled = true;
}
}
謝謝!我設法實現了這一點。我正在用完整的代碼編輯我的問題。 – etaiso 2014-09-22 13:25:00