隐藏一些datagridview复选框单元格

And*_*res 4 c# datagridview datagridviewcheckboxcell winforms

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

这是datagrid的一个屏幕:

在此输入图像描述

我的问题是我需要禁用付费插件的复选框.在这种情况下,当"Restante"(剩下要付钱)是= 0.

我读了一些他们使用paint事件的帖子没有显示复选框单元格,但我不喜欢那个解决方案.我想隐藏了复选框单元格,但我不知道是否可以这样做.

多数民众赞成我的尝试:

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;
                }
            }
Run Code Online (Sandbox Code Playgroud)

显然这不起作用,我得到一个编译器错误消息,说该属性是只读的.

有人知道如何将复选框单元格设置为不可见吗?

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

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;
Run Code Online (Sandbox Code Playgroud)

编辑:

一些考虑:

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

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);
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

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

小智 7

为复选框单元格指定值.然后将其转换为具有新值的TextBox.为我工作.

dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";
Run Code Online (Sandbox Code Playgroud)


spa*_*jce 5

是的,您可以通过转换为DataGridViewCheckBoxCell来完成此操作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();
            }
        }
Run Code Online (Sandbox Code Playgroud)