【问题标题】:Changing color of a Checkbox of a DataGridCheckBoxCell C# Winform更改 DataGridCheckBoxCell C# Winform 的复选框的颜色
【发布时间】:2013-01-15 20:42:11
【问题描述】:

我前段时间问过这个问题Hide some datagridview checkbox cell。我无法得到一个好的答案,所以我决定尝试一些不同的东西。

我有一个包含所有贷款分期付款的表格。每期都有一个复选框。当该复选框被选中时,我表示客户正在为此付费。

现在,当已支付分期付款时,不需要该复选框。现在,我禁用了分期付款复选框。它是可见的,但单击时它什么也不做。但我想做的是将复选框涂成绿色。就像在这张图片中一样(颜色是在 Paint 中绘制的,它不是真实的图像)

好的,我尝试在使用此代码之前隐藏复选框:

private void dgv_Cuotas_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {           
        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.Green;
                e.PaintBackground(e.ClipBounds, true);
                e.Handled = true;
            }
        }
    }

我用这个绘制整个单元格,但我只想绘制复选框。此外,通过这个实现,当我单击该单元格时,我可以注意到我正在单击一个复选框,并且背景颜色变为白色,如下所示:

我通过在运行时分配 Datasource = lista_cuota(分期付款列表)来创建 DataGridView (dgv_Cuotas) 行和列。这就是为什么通过代码在其后添加复选框列的原因:

DataGridViewCheckBoxColumn chbox = new DataGridViewCheckBoxColumn();
        {
            chbox.CellTemplate = new DataGridViewCheckBoxCell();
            chbox.HeaderText = "";
            chbox.Name = "Seleccionar";
            chbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            chbox.FlatStyle = FlatStyle.Standard;
        }
        dgv_Cuotas.Columns.Insert(16, chbox);
        dgv_Cuotas.Columns[16].DisplayIndex = 0;

这样做,我无法“隐藏”将 DataGridViewTextBoxCell 分配给特定单元格的复选框单元格,因此我想尝试仅绘制复选框。

由于我使用的是实体框架,我不想在类中添加新字段。

【问题讨论】:

    标签: c# winforms datagridview paint datagridviewcheckboxcell


    【解决方案1】:

    通过我所有的研究,我得出结论,改变 CheckBoxCell 的颜色是不可能的。我可以通过绘制一个相同大小的矩形来模拟它。我在另一个问题Drawing a filled circle or rectangle inside a DataGridViewCell in C# Winforms

    中写了这个解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2020-09-21
      • 2011-08-16
      • 1970-01-01
      相关资源
      最近更新 更多