I want to draw a small filled circle in the center of a DataGridViewCell
. A rectangle can do the trick as well. I assume I must do it in the CellPainting event.
I have 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;
}
}
Its painting the whole cell and I just want a small circle or rectangle as I show you in the next picture:
How can I achieve this? Using a DataGridViewImageCell is not an option because I´m having a formatting error. I just can change that DataGridViewCheckBoxCell to a DataGridViewTextboxCell.
EDIT: I can change it to DataGridViewImageCell!! Dont know what happened before, but I still can´t load the image there. 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;
Check out DataGridView templates in order to customize columns in this way. This will give you greater control.
This may help: http://csharp.net-informations.com/datagridview/csharp-datagridview-template.htm
Thank's for your question and answer @Andres .
Please see my response : (for example) I have a datagridview with 2 columns. In column first I want display a color circle that color of it is write(color name) in column 2. for this my code is :
For create a circle I write this class code:
In CellPainting event of my datagridview, write this code :
Result is datagridview with 2 columns :
column 1 : 6 circle with 6 specific color
column 2 : 6 color name
Thanks.
I finally resolved it. I drew a filled rectangle with the same size as the checkbox and in the same location.
I did the following:
First, I change the DataGridViewCheckBoxCell to DataGridViewTextBoxCell to hide the checkbox.
Be sure of selecting transparent forecolor so as not to see "False" in the Cell.
After that, I just painted the rectangle in the cell using the cellpainting event:
You can get the location you want by changing the Location.X and Location.Y values like I did.
Hope that helps somebody!