如何处理DataGridView编辑控件的绘画? [英] How do I handle painting for a DataGridView's editing control?
问题描述
我有一个 DataGridView
,我在其$ RowPostPaint $ c中的每行的第一个单元格上绘制TreeView样式的虚线$ c>事件。当第一个单元格(这是一个
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);
然后处理 CellPainting
事件并执行这些步骤:
Then handle CellPainting
event and do these steps:
- 仅绘制第一列,RowIndex应为> = 0以避免渲染列标题
- 画您的树线或任何你想要的
- 使用e.Handled = true取消默认绘画
这里是代码:
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屋!