C# 如何设置列标题中dividerwidth的颜色

C# 如何设置列标题中dividerwidth的颜色,c#,datagridview,datagridcolumnheader,C#,Datagridview,Datagridcolumnheader,我已经将分隔器宽度和分隔器高度设置为非零,然后使用dataGridview1.GridColor=Color.Red设置分隔器的颜色。但这并不影响标题。如何更改标题单元格之间间隙的颜色?;i、 e.我如何使该间隙也变为红色 更新:诀窍是允许在标题中应用您自己的样式。要执行此操作,需要此行关闭EnableHeaderVisualStyles标志: dataGridView1.EnableHeadersVisualStyles = false; 没有它,将应用用户设置。看 旧答案: 你总是可

我已经将分隔器宽度和分隔器高度设置为非零,然后使用
dataGridview1.GridColor=Color.Red
设置分隔器的颜色。但这并不影响标题。如何更改标题单元格之间间隙的颜色?;i、 e.我如何使该间隙也变为红色


更新:诀窍是允许在标题中应用您自己的样式。要执行此操作,需要此行关闭
EnableHeaderVisualStyles
标志:

  dataGridView1.EnableHeadersVisualStyles = false;
没有它,将应用用户设置。看


旧答案:

你总是可以通过绘制标题单元格来完成工作

下面是一个简短的例子:

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
}
private void dataGridView1\u CellPainting(对象发送方,DataGridViewCellPaintingEventArgs e)
{
如果(e.RowIndex>=0)返回;//仅列标题!
//系统仍然可以完成艰苦的工作:
e、 绘画背景(如CellBounds,真);
e、 绘画内容(如细胞边界);
//现在看标题中的行。。
矩形r=e.CellBounds;
使用(画笔pen0=新画笔(dataGridView1.GridColor,1))
{
//第一条垂直网格线:
如果(e.ColumnIndex<0)e.Graphics.DrawLine(pen0,r.X,r.Y,r.X,r.Bottom);
//每个单元格的右边框:
e、 图形.抽绳(笔尖0,右-1,右-Y,右-1,右下);
}
e、 Handled=true;//停止系统对标头的任何进一步工作
}

你看到了吗?是的,我一直在玩那个代码,但没有找到答案。(有趣的是,该示例代码省略了dataGridView1.EnableHeaderVisualStyles=false,这是必需的。)我指的是第一个注释,这对我来说意味着如果不关闭EnableHeaderVisualStyles,这是不可能的,谁会想要呢??所以我想你需要自己绘制列标题..查看我的更新!!