ms中的DataGridViewCell中绘制一个填充的圆或矩形

ms中的DataGridViewCell中绘制一个填充的圆或矩形

本文介绍了在C#Winforms中的DataGridViewCell中绘制一个填充的圆或矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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!不知道以前发生了什么,但我仍然无法加载图像。我只是得到一个红色十字架的白色方块(没有图像图标)。这是我的代码:

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;

确保选择透明的forecolor,以免在Cell中看到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.


希望这有助于某人!

Hope that helps somebody!

这篇关于在C#Winforms中的DataGridViewCell中绘制一个填充的圆或矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:03