这里的想法是我重新绘制了组合" cell"这样就可以显示颜色和文本块。这是表单显示时,它将显示下拉列表:
在我选择了一种颜色后,它很奇怪:
现在一切都错了。我必须将鼠标悬停在控件上以呈现其他位。只是没有正常工作。
我的经纪人:
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;
这改善了事物,因为它并没有那么奇怪。当然,它实际上并没有绘制任何东西。但是,对于最左边的单元格(带有编辑符号),只需要很长时间就可以显示为白色。所以它的机制仍然不对。 谢谢。
如果我按照建议的方式尝试:
取得进步!我们可以调整它仍然包括网格线吗?像在正常细胞中一样?
答案 0 :(得分:1)
之后
CellBounds
g.Dispose();
Paint
结果如下:
此private void dataGridView2_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 4 && e.RowIndex == 0) // use your own checks here!!
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.CellBounds;
rDraw.Inflate(-1, -1);
{
brBack = Brushes.White;
g.FillRectangle(brBack, rDraw); // **
}
try
{ // use your own code here again!
// ComboboxColorItem oColorItem =
// (ComboboxColorItem)((ComboBox)sender).SelectedItem;
s = "WW";// oColorItem.ToString();
c = Color.LawnGreen;// oColorItem.Value;
} catch
{
s = "red";
c = Color.Red;
}
// asuming a square is right; make it a few pixels smaller!
int butSize = e.CellBounds.Height;
Rectangle rbut = new Rectangle(e.CellBounds.Right - butSize ,
e.CellBounds.Top, butSize , butSize );
ComboBoxRenderer.DrawDropDownButton(e.Graphics, rbut,
System.Windows.Forms.VisualStyles.ComboBoxState.Normal);
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle( e.CellBounds.Left + 5,
e.CellBounds.Top + 3, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black,
e.CellBounds.Left + 25, e.CellBounds.Top + 1);
b.Dispose();
//g.Dispose(); <-- do not dispose of thing you have not created!
e.Handled = true;
}
}
事件:
CombBoxCell
请注意,我只有一个ComboboxColorItem
,因此我更改了检查。并且我没有// use your own code here again!
if(DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
ComboboxColorItem oColorItem = (ComboboxColorItem)DataGridView1
.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
s = oColorItem.ToString();
c = oColorItem.Value;
}
,所以我替换了一个随机字符串&amp;颜色。
从OP更新:我的一些语法错误且需要:
Core_Faculty