Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - Hide some datagridview checkbox cell

I have a datagridview showing installments of a loan. I created a datagridviewcheckbox column so then I can select all the installments i want to pay for.

This is a screen of the datagrid:

enter image description here

My issue is that I need to disable the checkboxes of the paid intallments. In this case, when "Restante" (what′s left to pay) is = 0.

I read some posts where they used the paint event to not show the checkbox cell, but i didnt like that solution. I thought of hiding the checkbox cell, but i don′t know if it is possible to do that.

Thats what i tried:

foreach (DataGridViewRow row in dgv_Cuotas.Rows)
            {
                if (Convert.ToDecimal(dgv_Cuotas.Rows[row.Index].Cells[17].Value) == 0)
                {
                    dgv_Cuotas.Rows[row.Index].Cells[16].Visible = false;
                }
            }

Obviously this does not works, I get a compiler error message saying that the property is read only.

Does somebody knows how to set the checkbox cell to invisible?

Just in case, I attach the DataGridViewCheckboxColumn creation 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;

EDIT:

Some considerations:

I use the cell content click event to handle the checkboxes, so readonly wont work. What I want is to hide the checkbox:

private void dgv_Cuotas_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1)
            return;
        if (dgv_Cuotas.Columns[e.ColumnIndex].Name == "Seleccionar")
        {
            DataGridViewRow row = dgv_Cuotas.Rows[e.RowIndex];
            DataGridViewCheckBoxCell cellSeleccion = row.Cells["Seleccionar"] as DataGridViewCheckBoxCell;
            int n_cuota = Convert.ToInt32(dgv_Cuotas[2, dgv_Cuotas.CurrentRow.Index].Value);
            Cuota cuota_seleccionada = new Cuota();
            cuota_seleccionada = Lista_cuotas.Where(x => x.num_cuota == n_cuota).First();

            if (cellSeleccion != null && Convert.ToBoolean(cellSeleccion.Value) == true)
            {
                cellSeleccion.Value = false;
                Actualizar_cuotas_seleccionadas(false, cuota_seleccionada);
            }
            else
            {
                if (cellSeleccion != null && Convert.ToBoolean(cellSeleccion.Value) == false)
                {
                    cellSeleccion.Value = true;
                    Actualizar_cuotas_seleccionadas(true, cuota_seleccionada);
                }
            }
        }

In the other hand, I′m already using the Onpaint event. Its inherited, thats why I′m trying to avoid using it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assign a value to the checkbox cell. Then Convert it to a TextBox with a new value. Worked for me.

dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...