在C# Winforms中DataGridViewCell内绘制填充圆形或矩形

9
我希望能在DataGridViewCell中央绘制一个小的填充圆。使用矩形也可以达到同样的效果。我假设我必须在CellPainting事件中完成它。
我尝试过以下代码:
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;
            }
        }

输入图像描述

它会涂满整个单元格,但我只想要一个小圆形或矩形,就像我在下一张图片中展示的那样:

输入图像描述

我该怎么做呢?使用DataGridViewImageCell不是一个选项,因为我有一个格式错误。我只能将DataGridViewCheckBoxCell更改为DataGridViewTextboxCell。

编辑: 我可以将其更改为DataGridViewImageCell!!之前发生了什么我不知道,但我仍然无法在那里加载图片。我只得到一个带有红色十字(没有图像图标)的白色正方形。这是我的代码:

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;

你能否只是扩展单选按钮控件的类,就像 https://dev59.com/AFjUa4cB1Zd3GeqPSpEc 中所示? - Tebc
3个回答

7

感谢您的问题和 @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;                
    }
}

结果是带有两列的数据网格视图:

第一列:6个带有6种特定颜色的圆形

第二列:6种颜色的名称

谢谢。


5
我终于解决了它。我画了一个与复选框大小相同且位置相同的填充矩形。
我做了如下操作:
首先,我将DataGridViewCheckBoxCell更改为DataGridViewTextBoxCell以隐藏复选框。
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”。

之后,我只需使用cellpainting事件在单元格中绘制矩形:

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值来获取所需的位置。希望这能帮助到某些人!enter image description here

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接