如何处理绘画的一个DataGridView的编辑控制? [英] How do I handle painting for a DataGridView's editing control?

查看:111
本文介绍了如何处理绘画的一个DataGridView的编辑控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的DataGridView 和我画的TreeView风格的虚线每排在其 RowPostPaint 事件。当第一小区(其是 DataGridViewTextBoxCell )是在编辑模式,该行不绘制。如何处理绘画的编辑控制?该标准的编辑控件没有一个Paint事件,我不希望创建一个新的类型的细胞,如果我能避免这样做。

I have a DataGridView and I'm drawing TreeView-style dotted lines on the first cell of each row during its RowPostPaint event. When the first cell (which is a DataGridViewTextBoxCell) is in editing mode, the lines aren't drawn. How do I handle painting for the editing control? The standard editing control doesn't have a Paint event, and I don't want to create a new type of cell if I can avoid doing so.

推荐答案

我觉得这就是答案:

左一集的第一列的单元格边距为16,所以在查看模式或编辑模式,内容会通过给定的填充显示。

First set cell padding of your first column to 16 from left, so in view mode or edit mode, content will be shown using given padding.

this.dataGridView1.Columns[0].DefaultCellStyle.Padding= new Padding(16,0,0,0);

然后处理细胞绘画活动,做以下步骤:

Then handle Cell Painting event and do these steps:

  1. 只画第一列rowIndex位置应为> = 0,以避免渲染列标题
  2. 在画你的树纹或任何你想
  3. 在使用e.Handled = TRUE
  4. 取消默认的画

下面是code:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    //Only paint rirst column and RowIndex should be >=0 to avoid rendering column header
    if (e.ColumnIndex == 0 & e.RowIndex >= 0)
    {
        //Paint your tree lines or whatever you want
        using (var treePen = new Pen(Color.Gray, 1))
        {
            treePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);
            e.Graphics.DrawLine(treePen,
                new Point(e.CellBounds.Left + 4, e.CellBounds.Top),
                new Point(e.CellBounds.Left + 4, e.CellBounds.Bottom));

            e.Graphics.DrawLine(treePen,
                new Point(e.CellBounds.Left + 4, e.CellBounds.Top + e.CellBounds.Height / 2),
                new Point(e.CellBounds.Left + 12, e.CellBounds.Top + e.CellBounds.Height / 2));
        }

        //Cancel default painting using e.Handled = true
        e.Handled = true;
    }
}

和这里是截图:

输入图像的描述在这里

这篇关于如何处理绘画的一个DataGridView的编辑控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆