Drawing a filled circle or rectangle inside a DataGridViewCell in C # Winforms

I want to draw a small filled circle in the center of the DataGridViewCell . A rectangle can also do the trick. I guess I should do this in the CellPainting event.

I tried this:

 if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { if (dgv_Cuotas.Columns[e.ColumnIndex].Name == "Seleccionar" && Convert.ToBoolean(dgv_Cuotas.Rows[e.RowIndex].Cells["pagada"].Value) == true) { e.CellStyle.BackColor = Color.LightGray; ; e.PaintBackground(e.ClipBounds, true); e.Handled = true; } } 

enter image description here

His picture is a whole cell, and I just want a small circle or rectangle, as I will show you in the following figure:

enter image description here

How can I achieve this? Using DataGridViewImageCell is not an option because I have a formatting error. I can just change the DataGridViewCheckBoxCell to a DataGridViewTextboxCell.

EDIT: I can change it to DataGridViewImageCell! I don’t know what happened before, but I still can’t upload the image. I just get a white square with a red cross (No image icon). Here is my code:

 dgv_Cuotas.Rows[row.Index].Cells["Seleccionar"] = new DataGridViewImageCell(); dgv_Cuotas.Rows[row.Index].Cells["Seleccionar"].Value = Properties.Resources.punto_verde; dgv_Cuotas.Rows[row.Index].Cells["Seleccionar"].Style.ForeColor = Color.White; dgv_Cuotas.Rows[row.Index].Cells["Seleccionar"].Style.SelectionForeColor = Color.White; 
+7
source share
3 answers

I finally decided. I drew a filled rectangle of the same size as the checkbox and in the same place.

I have done the following:

First, I change the DataGridViewCheckBoxCell to a DataGridViewTextBoxCell to hide this check box.

 DataGridViewTextBoxCell blank_cell = new DataGridViewTextBoxCell(); dgv_Cuotas.Rows[row.Index].Cells["Seleccionar"] = blank_cell; dgv_Cuotas.Rows[row.Index].Cells["Seleccionar"].Style.ForeColor = Color.Transparent; dgv_Cuotas.Rows[row.Index].Cells["Seleccionar"].Style.SelectionForeColor = Color.Transparent; 

Be sure to select a transparent forecolor so as not to see “False” in the cell.

After that, I just drew a rectangle in the cell using the cellpainting event:

 if (dgv_Cuotas.Columns[e.ColumnIndex].Name == "Seleccionar" && Convert.ToDecimal(dgv_Cuotas.Rows[e.RowIndex].Cells["Restante"].Value) == 0) { Color c1 = Color.FromArgb(255, 113, 255, 0); Color c2 = Color.FromArgb(255, 2, 143, 17); LinearGradientBrush br = new LinearGradientBrush(e.CellBounds, c1, c2, 90, true); ColorBlend cb = new ColorBlend(); cb.Positions = new[] { 0, (float)1 }; cb.Colors = new[] { c1, c2 }; br.InterpolationColors = cb; Rectangle rect = new Rectangle(e.CellBounds.Location.X + 4, e.CellBounds.Location.Y + 4, 13, 13); e.Graphics.FillRectangle(br, rect); e.PaintContent(rect); e.Handled = true; } 

You can get the desired location by changing the values ​​of Location.X and Location.Y, like me.

enter image description here Hope someone helps!

+4
source

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.

+5
source

Check out DataGridView templates to customize your columns this way. This will give you more control.

This may help: http://csharp.net-informations.com/datagridview/csharp-datagridview-template.htm

+1
source

All Articles