Question

I have asked this question some time ago Hide some datagridview checkbox cell. 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)

enter image description here

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

enter image description here

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:

enter image description here

I´m creating the DataGridView (dgv_Cuotas) Rows and Columns by assingning Datasource = lista_cuota (List of installments) during runtime. 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.

As I´m using Entity framework, I don´t want to add new fields to the class.

Was it helpful?

Solution

With all my research, I reach the conclusion that changing the color of the CheckBoxCell is not possible. 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top