WINFORM DataGridView表头边框颜色

列头的背景颜色与DataGridView.EnableHeadersVisualStyles属性,Cell.Style.BackColor
下面的代码示例演示如何禁用行和列标头的视觉样式在启用了视觉样式的应用程序。 在此示例中,可视样式为应用程序启用了通过调用 Main的 EnableVisualStyles 方法。 但是,在中,因为 EnableHeadersVisualStyles 设置为 false,视觉样式不会应用于行和列标头。 此代码示例摘自为 DataGridViewAdvancedBorderStyle 类提供的一个更大的示例。 
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.Columns[1].HeaderCell.Style.BackColor = Color.Red;
 

列的内表边框颜色与DataGridView.GridColor属性
获取和设置网格线的颜色,网格线对 DataGridView 的单元格进行分隔。
注:在使用单个边框时,可以将 GridColor 属性设置为任何颜色;对于其他类型的边框,颜色由操作系统指定。如果通过 Application.EnableVisualStyles 方法为应用程序启用了可视样式,将忽略 GridColor 属性,而使用当前主题的颜色。

外边框的颜色与Paint事件和ControlPaint.DrawBorder(Graphics,Rectangle,Color,int,ButtonBoardStyle,...)方法
必须实现设置BorderStyle.None;
容器的尺寸必须大于容器的内容,否则容器的边框将被覆盖导致无法显示。
该方式同样适用于Panel。
拖动滚动条会触发Paint事件导致线条乱入的问题。
private static void control_Paint(object sender, PaintEventArgs e)
{
        ControlPaint.DrawBorder(e.Graphics,
                            ((Panel)sender).ClientRectangle,
                            Color.FromArgb(170, 170, 170),
                            1,
                            ButtonBorderStyle.Solid,
                            Color.FromArgb(170, 170, 170),
                            1,
                            ButtonBorderStyle.Solid,
                            Color.FromArgb(170, 170, 170),
                            1,
                            ButtonBorderStyle.Solid,
                            Color.FromArgb(170, 170, 170),
                            1,
                            ButtonBorderStyle.Solid);
}

外边框的颜色与Graphics.DrawRectangle(Pen, Rectangle)方法
private static void dataGridView_Paint(object sender, PaintEventArgs e)
{
    Pen pen = new Pen(Color.FromArgb(170,170,170));
    e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, ((DataGridView)sender).Width - 1, ((DataGridView)sender).Height - 1));
}

posted @ 2017-09-05 09:37  sunlyk  阅读(3813)  评论(0编辑  收藏  举报