DataGridView 自定义绘制 RowHeader 需要增加事件 CellPainting +=DataGridView_CellPainting;
//行三角形 文章来源: codebye
void DataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//行头部的RowIndex为0 ColumnIndex为-1
if (e.RowIndex > -1 && e.ColumnIndex == -1 && Rows[e.RowIndex].Selected) //对选中行进行重绘
{
//距顶部距离
var topMargin = (e.RowIndex + 1) * RowTemplate.Height;
//绘制单元格底色
e.Graphics.FillRectangle(new SolidBrush(RowHeadersDefaultCellStyle.BackColor), new Rectangle(1, topMargin+2, e.CellBounds.Width – 2, e.CellBounds.Height-1));
//绘制单元格边框
e.Graphics.DrawLines(new Pen(GridColor), new Point[] { new Point(0, topMargin), new Point(0, topMargin + RowTemplate.Height+1), new Point(e.CellBounds.Width, topMargin + RowTemplate.Height+1) });
var point1 = new Point(3, topMargin + 4);
var point2 = new Point(3, topMargin + 16);
var point3 = new Point(9, topMargin + 10);
Point[] curvePoints = { point1, point2, point3 };
FillMode newFillMode = FillMode.Winding;
//绘制黑色三角形
e.Graphics.FillPolygon(Brushes.Black, curvePoints, newFillMode);
e.Handled = true;
}
}