如何在数据网格中使波浪线像拼写一样c#

时间:2015-03-24 15:08:54

标签: c# winforms

当用户制作新网格时,我想要显示如下的波浪线:

http://i.stack.imgur.com/hOTMa.png

我该怎么做? Currenlty,我通过调用DrawLine来绘制它,但它需要很长时间才能完成它。

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics l = e.Graphics;
        Pen p = new Pen(Color.Red, 1);
        l.DrawLine(p ,10 ,5,5,0);
        l.DrawLine(p, 10, 5, 15, 0);
        l.DrawLine(p, 20, 5, 15, 0);
        l.DrawLine(p, 20, 5, 25, 0);
        l.DrawLine(p, 30, 5, 25, 0);
        l.Dispose();
    }

1 个答案:

答案 0 :(得分:1)

DataGridView.CellPainting事件添加处理程序。

dataGridView1.CellPainting += dataGridView1_CellPainting;

这里实施。注意:这些行位于“白色框”之外。细胞。

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    var dg = (DataGridView) sender;

    if (e.ColumnIndex == -1 || e.RowIndex != (dg.RowCount - 1))
        return;

    using (var p = new Pen(Color.Red, 1))
    {
        var cellBounds = e.CellBounds;

        const int size = 2;
        var pts = new List<Point>();
        var h = false;
        for (int i = cellBounds.Left; i <= cellBounds.Right; i += size,h = !h)
        {
            pts.Add(
                new Point
                {
                    X = i,
                    Y = h ? cellBounds.Bottom : cellBounds.Bottom + size
                });
        }

        e.Graphics.DrawLines(p, pts.ToArray());
    }
}

输出:

DataGridView

我希望它有所帮助。