And*_*res 9 c# geometry datagridview draw winforms
我想在一个中心画一个小圆圈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;
}
}
Run Code Online (Sandbox Code Playgroud)
它绘制了整个单元格,我只想要一个小圆圈或矩形,如下图所示:
我怎样才能做到这一点?使用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;
Run Code Online (Sandbox Code Playgroud)
感谢您的问题和答案@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";
Run Code Online (Sandbox Code Playgroud)
为了创建一个圆圈,我编写了这个类代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
结果是带有2列的datagridview:
第1列:6圈,具有6种特定颜色
第2列:6色名称
谢谢.
我终于解决了.我绘制了一个填充矩形,其大小与复选框相同,位于相同位置.
我做了以下事情:
首先,我将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;
Run Code Online (Sandbox Code Playgroud)
一定要选择透明的前色,以免在单元格中看到"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;
}
Run Code Online (Sandbox Code Playgroud)
您可以像我一样更改Location.X和Location.Y值,从而获得所需的位置.
希望能帮到别人!