感谢您的提问和回答@Andres。
请看我的回复:
(例如)我有一个包含 2 列的 datagridview。在第一列中,我想显示一个色环,其颜色在第二列中写入(颜色名称)。为此,我的代码是:
for (int i = 1; i <= 5; i++)
Dgv.Rows.Add();
Dgv[1, 0].Value = "Red";
Dgv[1, 1].Value = "Blue";
Dgv[1, 2].Value = "Yellow";
Dgv[1, 3].Value = "Green";
Dgv[1, 4].Value = "Black";
为了创建一个圆圈,我编写了此类代码:
public static class GraphicsExtensions
{
public static void FillCircle(this Graphics g, Brush brush, float centerX, float centerY, float radius)
{
g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
}
在我的 datagridview 的 CellPainting 事件中,编写以下代码:
private void Dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1)
{
Brush Brs= new SolidBrush(Color.FromName(Dgv[1, e.RowIndex].Value.ToString()));
GraphicsExtensions.FillCircle(e.Graphics, Brs, e.CellBounds.Location.X + 5, e.CellBounds.Location.Y + 10, 5);
e.Handled = true;
}
}
结果是包含 2 列的 datagridview:
第 1 列:6 个圆圈,有 6 种特定颜色
第 2 列:6 种颜色名称
Thanks.