[英]How to change datagridview column divider color c# winforms
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.. 首先,我尝试更改dataGridView1.BackgroundColor,dataGridView1.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: 您需要处理
CellPainting
事件,并用所需的颜色(例如与GridColor
相同的颜色)填充背景,然后通过将绘制区域限制为不包括分隔线的矩形来执行其余绘制:
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;
}
}
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: 例如,如果将各列的
DividerWidth
设置为10,并将GridColor
设置为Color.Red
,则可以使用上面的代码获得以下结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.