1

我已将分隔线宽度和分隔线高度设置为非零,然后用于dataGridview1.GridColor = Color.Red设置分隔线的颜色。不过,这不会影响标题。如何更改标题单元格之间间隙的颜色?即我怎样才能使那个间隙也变成红色?

带有白色分隔线的数据网格示例

4

1 回答 1

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
}

在此处输入图像描述

于 2017-05-23T15:14:46.573 回答