如何更改Datagridview列分隔符颜色C#WinForms [英] How to change datagridview column divider color 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屋!