2017-05-23 39 views
1

我已經將divider width和divider height設置爲非零值,然後使用dataGridview1.GridColor = Color.Red來設置分隔符的顏色。不過這並不影響標題。我怎樣才能改變標題單元之間的間隙的顏色?即我怎樣才能使這個差距紅?如何在列標題中設置dividerwidth的顏色

datagrid example with white divider

+0

你看到[這](https://msdn.microsoft.com/en-us/library/system.windows.forms。 datagridview.columnheadersborderstyle%28v = vs.110%29.aspx)? – TaW

+0

是的,我沒有找到答案玩過這段代碼。 (有趣的是,示例代碼省略了dataGridView1.EnableHeadersVisualStyles = false,這是需要的。) – 4mla1fn

+0

我的意思是第一個音符,對我來說這意味着不可能不關閉EnableHeadersVisualStyles,誰會想要?所以我想你需要自己畫柱頭。 – TaW

回答

1

更新:訣竅是讓你自己的風格在頭應用。要做到這一點,你需要這條線關閉EnableHeadersVisualStyles標誌:

dataGridView1.EnableHeadersVisualStyles = false; 

沒有它應用的用戶設置。見MSDN


老答案:

你總是可以通過所有者繪製頭細胞做的東西。

下面是一個簡單的例子:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.RowIndex >= 0) return; // only the column headers! 
    // the hard work still can be done by the system: 
    e.PaintBackground(e.CellBounds, true); 
    e.PaintContent(e.CellBounds); 
    // now for the lines in the header.. 
    Rectangle r = e.CellBounds; 
    using (Pen pen0 = new Pen(dataGridView1.GridColor, 1)) 
    { 
     // first vertical grid line: 
     if (e.ColumnIndex < 0) e.Graphics.DrawLine(pen0, r.X, r.Y, r.X, r.Bottom); 
     // right border of each cell: 
     e.Graphics.DrawLine(pen0, r.Right - 1, r.Y, r.Right - 1, r.Bottom); 
    } 
    e.Handled = true; // stop the system from any further work on the headers 
} 

enter image description here

相關問題