在C#Winforms中的DataGridViewCell内绘制填充圆或矩形
我想在在C#Winforms中的DataGridViewCell内绘制填充圆或矩形,c#,winforms,datagridview,draw,geometry,C#,Winforms,Datagridview,Draw,Geometry,我想在DataGridViewCell的中心画一个小的填充圆。一个矩形也可以做到这一点。我想我必须在绘画比赛中这样做 我试过这个: if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { if (dgv_Cuotas.Columns[e.ColumnIndex].Name == "Seleccionar" && Convert.ToBoolean(
DataGridViewCell
的中心画一个小的填充圆。一个矩形也可以做到这一点。我想我必须在绘画比赛中这样做
我试过这个:
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;
查看DataGridView模板,以便以这种方式自定义列。这将给你更大的控制 这可能有助于:
我终于解决了这个问题。我在同一位置绘制了一个与复选框大小相同的填充矩形 我做了以下工作: 首先,我将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”
之后,我使用CellPaint事件在单元格中绘制矩形:
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值,可以获得所需的位置
希望这能帮助别人 谢谢你的提问和回答@Andres 请看我的答复: (例如)我有一个带有2列的datagridview。在第一列中,我想在第2列中显示一个颜色圈,其颜色为write(颜色名称)。为此,我的代码是:
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";
在my datagridview的事件中,编写以下代码:
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;
}
}
结果是datagridview,包含两列:
第1列:6个带6种特定颜色的圆圈
第2列:6个颜色名称
谢谢。您能像中一样扩展单选按钮控件的类吗
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;
}
}