在C#Winforms中的DataGridViewCell内绘制一个实心圆或矩形

时间:2013-01-16 00:41:40

标签: c# winforms datagridview draw geometry

我想在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;
            }
        }

enter image description here

它绘制整个单元格,我只想要一个小圆圈或矩形,如下图所示:

enter image description here

我怎样才能做到这一点? 使用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;

3 个答案:

答案 0 :(得分: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 :(得分:5)

感谢您的回答@Andres。

请参阅我的回复: (例如)我有一个包含2列的datagridview。在第一列中,我希望在第2列中显示一个颜色圆圈,其颜色为写入(颜色名称)。对于此,我的代码是:

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;                
    }
}

结果是带有2列的datagridview:

第1列:6圈,具有6种特定颜色

第2栏:6色名称

感谢。

答案 2 :(得分:1)

查看DataGridView模板,以便以这种方式自定义列。这将使您获得更大的控制权。

这可能会有所帮助: http://csharp.net-informations.com/datagridview/csharp-datagridview-template.htm