这里的想法是,我重绘了combol“cell”,以便显示颜色和文本块。这是表格显示时,它是要告诉下拉:我已经选择了颜色在DataGridView中使用CellPaint事件处理程序
后它怪异:
现在是完全错误的。我必须将鼠标悬停在控件上以呈现其他位。只是不正确。
我的处理程序:
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
@LarsTech,我从这里得到:http://stackoverflow.com/questions/7482534/custom-draw-of-datagridviewcomboboxcolumn –
我将不得不重新回答我的答案。 :-) – LarsTech