我正在使用.NET Framework 4.6创建一个UserControl。
我希望我的 DataGridView 中单元格的背景色部分为一种颜色,部分为另一种颜色(即单元格包含一条曲线;在曲线的右侧它是黑色的。在左侧,它是白色的(。通常,我会使用以下方法来更改单元格的背景颜色:
dataGridView1.Rows[row].Cells[column].Style.BackColor = Color.Red;
我考虑过的一个想法是将 GraphicsPath 对象添加到 DataGridViewCell,但是我已经浏览了这些方法,但没有找到这样做的方法。
如果您对如何做到这一点有任何想法,请告诉我。
如果我找不到这样做的方法,我可能只会创建一个 GraphicsPath 对象数组。也欢迎其他选择。
使用DataGridView
类CellPainting
事件,您可以为单元格绘制任何类型的背景
例
private void DataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
var cellBounds = e.CellBounds;
// Left part of cell
cellBounds.Width = cellBounds.Width / 2;
e.CellStyle.BackColor = Color.Black;
e.CellStyle.ForeColor = Color.White;
e.Graphics.SetClip(cellBounds);
e.PaintBackground(e.ClipBounds, true);
// draw all parts except background
e.Paint(e.CellBounds,
DataGridViewPaintParts.All & (~DataGridViewPaintParts.Background));
// Right part of cell
cellBounds.X = cellBounds.Right;
e.CellStyle.BackColor = Color.White;
e.CellStyle.ForeColor = Color.Black;
e.Graphics.SetClip(cellBounds);
e.PaintBackground(e.ClipBounds, true);
// draw all parts except background
e.Paint(e.CellBounds,
DataGridViewPaintParts.All & (~DataGridViewPaintParts.Background));
e.Handled = true;
}