在DataGridView单元格中绘制一条水平线

时间:2014-09-30 11:57:49

标签: c# winforms

这是我的panel1绘画事件

Pen graphPen = new Pen(Color.White, 10);
PointF pt1D = new PointF();
PointF pt2D = new PointF();
pt1D.X = 5;
pt1D.Y = 10;
pt2D.X = 175;
pt2D.Y = 10;

e.Graphics.DrawLine(graphPen, pt1D, pt2D);
e.Graphics.DrawLine(graphPen, 5, 10, 175, 10);

帮我修复datagridview单元格绘制事件的相同方法。 我想在datagridview单元格上绘制画线

1 个答案:

答案 0 :(得分:1)

这对真正的应用程序来说并不好,但是如果你必须这样做,你可以使用这个

//subscribing the event
dataGridView1.CellPainting += dataGridView1_CellPainting;

//handle the event
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{

    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        e.Graphics.DrawLine(Pens.Red, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Bottom);
        e.Graphics.DrawLine(Pens.Blue, e.CellBounds.Left, e.CellBounds.Top+e.CellBounds.Height / 2, e.CellBounds.Right,e.CellBounds.Top+ e.CellBounds.Height / 2);

        e.Paint(e.ClipBounds, DataGridViewPaintParts.ContentForeground);
        e.Handled = true;
    }
}

,结果与此相同:

enter image description here

但是,正如我之前所说,不要将其用于实际应用