DataGridView 自定义绘制 RowHeader 选中行显示三角形

.Net技术 码拜 8年前 (2016-08-30) 3648次浏览 0个评论

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;

}
}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明DataGridView 自定义绘制 RowHeader 选中行显示三角形
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!