【发布时间】:2015-02-12 01:02:44
【问题描述】:
显示上述问题的图片:
这是我用来在选定单元格周围绘制边框的代码:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex > 0 && e.RowIndex > -1)
{
if (e.Value != null && (!e.Value.Equals("0")) && (!e.Value.Equals("-")))
{
double d = Convert.ToDouble(e.Value);
if (e.ColumnIndex == 2)
{
int limit = Convert.ToInt16(numericUpDown1.Value);
if (d > limit)
{
int pWidth = 1;
Pen p = new Pen(Color.Red, pWidth);
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
int x = e.CellBounds.Left – pWidth;
int y = e.CellBounds.Top – pWidth;
int w = e.CellBounds.Width;
int h = e.CellBounds.Height;
e.Graphics.DrawRectangle(p, x,y,w,h);
e.Handled = true;
}
}
}
}
}
有什么办法让它们不消失吗?它不会发生在底部和右侧边框上。我尝试了几件事,包括:
- 禁用边框并为所有单元格绘制我自己的边框(同样的问题)
- 将绘制矩形调整为在单元格内(不喜欢外观)
- 将 CellEnter/CellLeave/CellClick 处理为 .Invalidate 行和列以尝试让自定义边框单元格在顶部重新绘制
【问题讨论】:
标签: c# winforms datagridview