如何更改Datagridview列分隔符颜色C#WinForms [英] How to change datagridview column divider color c# winforms

查看:42
本文介绍了如何更改Datagridview列分隔符颜色C#WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我尝试更改dataGridView1.BackgroundColor,dataGridView1.GridColor,但是没有用..然后,我尝试了dataGridView1.EnableHeadersVisualStyles = false dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.White,但对我没有任何帮助.

First i tried changing dataGridView1.BackgroundColor, dataGridView1.GridColor but didn't worked.. then i tried dataGridView1.EnableHeadersVisualStyles = false dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.White but nothing worked for me..

推荐答案

您需要处理 CellPainting 事件,并用所需的颜色填充背景,例如与 GridColor相同的颜色,然后通过将绘制区域限制为不包含分隔线的矩形来执行其余绘制:

You need to handle CellPainting event and fill the background with desired color, for example the same color as GridColor, then perform the rest of painting by limiting the paint area to a rectangle excluding divider:

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex == -1)
    {
        var dgv = (DataGridView)sender;
        var r = e.CellBounds;
        var w = 0;
        if (e.ColumnIndex > -1)
        {
            w = dgv.Columns[e.ColumnIndex].DividerWidth;
            r.Width = r.Width - w;
        }
        e.Graphics.SetClip(r);
        e.Paint(r, DataGridViewPaintParts.All);
        e.Graphics.SetClip(e.CellBounds);
        if (w > 0)
        {
            r = new Rectangle(r.Right - 1, r.Top, w + 1, r.Height);
            using (var brush = new SolidBrush(dgv.GridColor))
                e.Graphics.FillRectangle(brush, r);
        }
        e.Handled = true;
    }
}

例如,如果将列的 DividerWidth 设置为10,并将 GridColor 设置为 Color.Red ,则可以使用以下方法获得以下结果:上面的代码:

For example, if you set DividerWidth for the columns to 10 and set GridColor to Color.Red you can get the following result using above code:

这篇关于如何更改Datagridview列分隔符颜色C#WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆