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.
我有一个DataGridView,在它的RowPostPaint事件期间,我在每一行的第一个单元格上绘制树视图样式的虚线。当第一个单元格(即DataGridViewTextBoxCell)处于编辑模式时,不会绘制行。如何处理编辑控件的绘画?标准的编辑控件没有一个绘图事件,我不想创建一个新的类型的单元格,如果我可以避免这样做的话。
3 个解决方案
#1
3
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.
首先将第一列的单元格填充从左侧设置为16,因此在视图模式或编辑模式中,内容将使用给定的填充显示。
this.dataGridView1.Columns[0].DefaultCellStyle.Padding= new Padding(16,0,0,0);
Then handle CellPainting
event and do these steps:
然后处理CellPainting事件并执行以下步骤:
- Only paint first column and RowIndex should be >=0 to avoid rendering column header
- 只绘制第一列和RowIndex应该是>=0,以避免呈现列标题。
- Paint your tree lines or whatever you want
- 画上你的树形线或者你想要的任何东西
- Cancel default painting using e.Handled = true
- 使用e取消默认绘制。处理= true
Here is the 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;
}
}
and here is the screenshot:
这是截屏:
#2
2
I solved a similar problem by creating a custom cell type, and shrinking the editing control as Bryan described. It's not terribly difficult, and it's the only way I'm aware of to keep the editing control from drawing on top of everything.
我通过创建自定义单元格类型并缩小编辑控件(如Bryan所述)来解决类似的问题。这并不难,而且这是我所知道的防止编辑控件在所有东西之上绘制的唯一方法。
Something like this ought to work for you:
像这样的东西应该对你有用:
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;
}
}
}
#3
0
Try to handle DataGridView.CellPainting event.
试着处理DataGridView。CellPainting事件。
#1
3
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.
首先将第一列的单元格填充从左侧设置为16,因此在视图模式或编辑模式中,内容将使用给定的填充显示。
this.dataGridView1.Columns[0].DefaultCellStyle.Padding= new Padding(16,0,0,0);
Then handle CellPainting
event and do these steps:
然后处理CellPainting事件并执行以下步骤:
- Only paint first column and RowIndex should be >=0 to avoid rendering column header
- 只绘制第一列和RowIndex应该是>=0,以避免呈现列标题。
- Paint your tree lines or whatever you want
- 画上你的树形线或者你想要的任何东西
- Cancel default painting using e.Handled = true
- 使用e取消默认绘制。处理= true
Here is the 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;
}
}
and here is the screenshot:
这是截屏:
#2
2
I solved a similar problem by creating a custom cell type, and shrinking the editing control as Bryan described. It's not terribly difficult, and it's the only way I'm aware of to keep the editing control from drawing on top of everything.
我通过创建自定义单元格类型并缩小编辑控件(如Bryan所述)来解决类似的问题。这并不难,而且这是我所知道的防止编辑控件在所有东西之上绘制的唯一方法。
Something like this ought to work for you:
像这样的东西应该对你有用:
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;
}
}
}
#3
0
Try to handle DataGridView.CellPainting event.
试着处理DataGridView。CellPainting事件。