Thanks for your question and answer @Andres.
Please see my answer: (for example) I have a datagridview with 2 columns. In the first column I want to display a color wheel whose color is the entry (color name) in column 2. for this, my code is:
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";
To create a circle, I write this class code:
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); } }
In the CellPainting event of my datagridview, write this code:
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; } }
The result is a datagridview with two columns:
column 1: 6 circle with 6 specific colors
column 2: 6 color name
Thanks.
Ali Ahmadi
source share