【发布时间】:2016-06-07 16:22:39
【问题描述】:
这里的想法是我重新绘制组合“单元格”,以便它显示颜色和文本块。这是表单显示并且即将显示下拉列表的时候:
在我选择了一种颜色之后,它会变得很奇怪:
现在一切都错了。我必须将鼠标悬停在控件上才能呈现其他位。只是工作不正常。
我的处理程序:
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if(e.ColumnIndex == 0 && e.RowIndex > 0)
{
e.PaintBackground(e.ClipBounds, true);
e.PaintContent(e.ClipBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.ClipBounds;
rDraw.Inflate(-1, -1);
{
brBack = Brushes.White;
g.FillRectangle(brBack, e.ClipBounds);
}
try
{
ComboboxColorItem oColorItem = (ComboboxColorItem)((ComboBox)sender).SelectedItem;
s = oColorItem.ToString();
c = oColorItem.Value;
}
catch
{
s = "red";
c = Color.Red;
}
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle(e.ClipBounds.Left + 5, e.ClipBounds.Top + 3, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black, e.ClipBounds.Left + 25, e.ClipBounds.Top + 1);
b.Dispose();
g.Dispose();
e.Handled = true;
}
}
}
我有什么遗漏吗?一定是的。
更新:
我在 CellPainting 活动中尝试过这个:
if(e.ColumnIndex == 0 && e.RowIndex > 0)
{
using (Graphics g = e.Graphics)
{
g.FillRectangle(Brushes.Aqua, e.CellBounds);
}
}
else
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
}
e.Handled = true;
从某种意义上说,这改善了事情,不会变得那么奇怪。当然,它实际上并没有绘制任何东西。但是,最左边的单元格(带有编辑符号)只显示白色并不需要很长时间。所以它的机制仍然不正确。 谢谢。
如果我按照建议的方式尝试,我最终会得到:
取得了进展!我们可以调整它以仍然包含网格线吗?像在正常细胞中一样?
【问题讨论】:
-
绝对不要使用 ClipBounds。
-
@LarsTech,我是从这里得到的:stackoverflow.com/questions/7482534/…
-
我将不得不重新审视我的答案。 :-)
-
@LarsTech 我尝试了 CellBounds,但我仍然得到了所有其他控件执行 doo-lally 的筛分效应。作为旁注,我还需要绘制下拉组合箭头,但根据其他问题不知道该怎么做。
-
是的,你是对的。对不起..请看我的回答;一个严重的错误是删除您没有创建的 Graphics 对象;这很有趣,但似乎 DGV 在循环单元格时正在缓存它..
标签: c# winforms paint datagridviewcomboboxcell