简体   繁体   English

更改DataGridCheckBoxCell C#Winform复选框的颜色

[英]Changing color of a Checkbox of a DataGridCheckBoxCell C# Winform

I have asked this question some time ago Hide some datagridview checkbox cell . 我前段时间已经问过这个问题, 隐藏一些datagridview复选框单元格 I couldn´t get a good answer, so I decided to try something different. 我没有一个好的答案,所以我决定尝试一些不同的方法。

I have a grid with all the installments of a loan. 我有一个包含所有分期付款贷款的表格。 Every installment has a checkbox. 每期都有一个复选框。 When that checkbox is checked, I´m indicating that the client is paying for that one. 选中该复选框后,即表示客户正在为此支付费用。

Now, when a installment is already paid, the checkbox is not needed. 现在,当已经分期付款时,不需要此复选框。 Right now, I disabled the checkbox for payed installments. 现在,我禁用了分期付款的复选框。 It´s visible, but when clicked it does nothing. 它是可见的,但是单击它不会执行任何操作。 But i wanted to do is to paint the checkbox in green. 但是我要做的是将复选框涂成绿色。 Like in this image (the color was painted in Paint, its not a real image) 就像这张图片一样(颜色是用Paint绘制的,不是真实的图片)

在此处输入图片说明

Ok, I have tried to hide the checkbox before using this code: 好的,在使用此代码之前,我试图隐藏复选框:

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

在此处输入图片说明

With this I paint the whole cell, but I just want to paint the checkbox. 这样我可以绘制整个单元格,但是我只想绘制复选框。 Also, with this implementation, when I click that cell, I can notice I´m clicking a checkbox, and the background color turns white, like this: 同样,在此实现中,当我单击该单元格时,我会注意到我在单击一个复选框,并且背景颜色变为白色,如下所示:

在此处输入图片说明

I´m creating the DataGridView (dgv_Cuotas) Rows and Columns by assingning Datasource = lista_cuota (List of installments) during runtime. 我在运行时通过设置Datasource = lista_cuota(分期付款清单)来创建DataGridView(dgv_Cuotas)行和列。 Thats why the checkbox column is added after it by code: 这就是为什么在代码后添加复选框列的原因:

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;

Doing it this way, I cant "hide" the checkbox cell assigning a DataGridViewTextBoxCell to a specific cell, so I wanted to try painting just the checkbox instead. 这样,我无法“隐藏”将DataGridViewTextBoxCell分配给特定单元格的复选框单元格,因此我想尝试只绘制复选框。

As I´m using Entity framework, I don´t want to add new fields to the class. 当我使用实体框架时,我不想在类中添加新字段。

With all my research, I reach the conclusion that changing the color of the CheckBoxCell is not possible. 通过所有的研究,我得出的结论是无法更改CheckBoxCell的颜色。 I could simulate it by drawing a rectangle of the same size. 我可以通过绘制相同大小的矩形来模拟它。 I wrote that solution in another of my questions Drawing a filled circle or rectangle inside a DataGridViewCell in C# Winforms 我在另一个问题中写了该解决方案, 在C#Winforms的DataGridViewCell中绘制填充的圆形或矩形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM