如题:
在datagridview的cellpainting事件中添加如下代码,即可实现。
if (e.RowIndex != -1 && e.RowIndex < 3)
{Brush gridBrush = new SolidBrush(dgvDiseaseList.GridColor);
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds);
//e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(new Pen(Color.Black, 2), e.CellBounds.Left, e.CellBounds.Bottom, e.CellBounds.Left, e.CellBounds.Top);
e.Graphics.DrawLine(new Pen(Color.Black, 2), e.CellBounds.Left, e.CellBounds.Bottom, e.CellBounds.Right, e.CellBounds.Bottom);
e.Graphics.DrawLine(new Pen(Color.Black, 2), e.CellBounds.Right, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Bottom);
e.Graphics.DrawLine(new Pen(Color.Black, 2), e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Top);
if (e.Value != null)
{
e.Graphics.DrawString((e.Value).ToString(), e.CellStyle.Font, new SolidBrush(Color.Black), e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}