如何处理DataGridView编辑控件的绘图?

时间:2009-09-07 13:33:48

标签: c# .net winforms datagridview

我有一个DataGridView,我在RowPostPaint事件期间在每行的第一个单元格上绘制了TreeView样式的虚线。当第一个单元格(DataGridViewTextBoxCell)处于编辑模式时,不绘制线条。如何处理编辑控件的绘画?标准编辑控件没有Paint事件,如果可以避免,我不想创建新类型的单元格。

3 个答案:

答案 0 :(得分:3)

首先将第一列的单元格填充从左侧设置为16,因此在查看模式或编辑模式下,将使用给定的填充显示内容。

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

然后处理CellPainting事件并执行以下步骤:

  1. 仅绘制第一列,RowIndex应为> = 0以避免呈现列标题
  2. 绘制树线或任何你想要的
  3. 使用e.Handled = true
  4. 取消默认绘画

    以下是代码:

    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;
        }
    }
    

    以下是截图:

    enter image description here

答案 1 :(得分:2)

我通过创建自定义单元格类型解决了类似的问题,并像Bryan描述的那样缩小了编辑控件。它并不是非常困难,而且这是我所知道的唯一方法,可以让编辑控件不会在所有内容之上绘制。

这样的事应该适合你:

public class PaintAccommodatingTextBoxCell : DataGridViewTextBoxCell
{
    // Adjust the editing panel, so that custom painting isn't
    // drawn over when cells go into edit mode.
    public override Rectangle PositionEditingPanel(Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
    {
        // First, let base class do its adjustments
        Rectangle controlBounds = base.PositionEditingPanel(cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);

        // Shrink the bounds here...

        return controlBounds;
    }
}

public class PaintAccommodatingTextBoxColumn : DataGridViewTextBoxColumn
{
    PaintAccommodatingTextBoxCell templateCell;

    public PaintAccommodatingTextBoxColumn()
    {
        templateCell = new PaintAccommodatingTextBoxCell();
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return templateCell;
        }
        set
        {
            PaintAccommodatingTextBoxCell newTemplate = value as PaintAccommodatingTextBoxCell;
            if (newTemplate == null)
                throw new ArgumentException("Template must be a PaintAccommodatingTextBoxCell");
            else
                templateCell = newTemplate;
        }
    }
}

答案 2 :(得分:0)

尝试处理 DataGridView.CellPainting 事件。