在 C# Winforms 中的 DataGridViewCell 内绘制实心圆或矩形 [英] Drawing a filled circle or rectangle inside a DataGridViewCell in C# Winforms
问题描述
我想在 DataGridViewCell
的中心画一个小的实心圆.矩形也可以做到这一点.我想我必须在 CellPainting 事件中做到这一点.
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.
我已经试过了:
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:
我怎样才能做到这一点? 使用 DataGridViewImageCell 不是一种选择,因为我遇到了格式错误.我只需将 DataGridViewCheckBoxCell 更改为 DataGridViewTextboxCell.
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.
我可以把它改成 DataGridViewImageCell !!不知道之前发生了什么,但我仍然无法在那里加载图像.我只得到一个带红十字的白色方块(无图像图标).这是我的代码:
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;
推荐答案
我终于解决了.我在同一个位置画了一个和复选框一样大小的实心矩形.
I finally resolved it. I drew a filled rectangle with the same size as the checkbox and in the same location.
我做了以下事情:
首先,我将 DataGridViewCheckBoxCell 更改为 DataGridViewTextBoxCell 以隐藏复选框.
First, I change the DataGridViewCheckBoxCell to DataGridViewTextBoxCell to hide the checkbox.
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;
一定要选择透明的前景色,以免在单元格中看到False".
Be sure of selecting transparent forecolor so as not to see "False" in the Cell.
在那之后,我只是使用 cellpainting 事件在单元格中绘制了矩形:
After that, I just painted the 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;
}
您可以像我一样通过更改 Location.X 和 Location.Y 值来获取您想要的位置.
You can get the location you want by changing the Location.X and Location.Y values like I did.
希望对某人有所帮助!
这篇关于在 C# Winforms 中的 DataGridViewCell 内绘制实心圆或矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!