我有一个 DataGridView
,在每行的第一个单元格上绘制类似于TreeView的虚线。这是在RowPostPaint
事件中完成的。当第一个单元格(即DataGridViewTextBoxCell
)处于编辑模式时,这些线条将不会被绘制。如何处理编辑控件的绘制?标准的编辑控件没有Paint事件,如果可以避免,我不想创建新类型的单元格。
我有一个 DataGridView
,在每行的第一个单元格上绘制类似于TreeView的虚线。这是在RowPostPaint
事件中完成的。当第一个单元格(即DataGridViewTextBoxCell
)处于编辑模式时,这些线条将不会被绘制。如何处理编辑控件的绘制?标准的编辑控件没有Paint事件,如果可以避免,我不想创建新类型的单元格。
首先将第一列单元格的左边距设置为16,这样在查看模式或编辑模式下,内容将使用给定的填充显示。
this.dataGridView1.Columns[0].DefaultCellStyle.Padding= new Padding(16,0,0,0);
然后处理CellPainting
事件并执行以下步骤:
下面是代码:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//Only paint first 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;
}
}
这里是屏幕截图:
e.Handled
设置为 true 是危险的。设置 DataGridViewTextBoxCell 的值会覆盖它,调整大小不会重绘所有内容,留下一大堆混乱。更好的解决方案在这里:https://dev59.com/XGMm5IYBdhLWcg3wTtfi#35553181 - rémy我曾通过创建自定义单元格类型并像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;
}
}
}