隐藏一些 datagridview 复选框单元格 [英] Hide some datagridview checkbox cell

查看:78
本文介绍了隐藏一些 datagridview 复选框单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 datagridview 显示贷款的分期付款.我创建了一个 datagridviewcheckbox 列,这样我就可以选择我想要支付的所有分期付款.

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.

这是数据网格的屏幕:

我的问题是我需要禁用已付款项的复选框.在这种情况下,当Restante"(还剩下什么)是 = 0 时.

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.

这就是我试过的:

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?

为了以防万一,我附上 DataGridViewCheckboxColumn 创建代码:

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;

一些注意事项:

我使用单元格内容点击事件来处理复选框,所以 readonly 不起作用.我想要的是隐藏复选框:

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

另一方面,我已经在使用 Onpaint 事件.它是继承而来的,这就是为什么我试图避免使用它.

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

推荐答案

是的,您可以通过将 DataGridViewCheckBoxCell 转换为 DataGridViewTextBoxCell

Yes, you can do this by Converting the DataGridViewCheckBoxCell to DataGridViewTextBoxCell

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (dataGridView1.Rows[row.Index].Cells[17].EditedFormattedValue.ToString().Length == 0) //  if (string.IsNullOrWhiteSpace(dataGridView1.Rows[row.Index].Cells[4].EditedFormattedValue.ToString()))
                break; 
            if (Convert.ToDecimal(dataGridView1.Rows[row.Index].Cells[17].EditedFormattedValue) == 0)
            {
                dataGridView1.Rows[row.Index].Cells[16].Value = null;
                dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();

            }
            else
            {
                //dgv_Cuotas.Rows[row.Index].Cells[16] = new DataGridViewCheckBoxCell();
            }
        }

这篇关于隐藏一些 datagridview 复选框单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆