隐藏一些datagridview复选框单元格

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

这是datagrid的一个屏幕:

在此处输入图像描述

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

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

多数民众赞成我尝试过:

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

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

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

为了以防万一,我附加了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; 

编辑:

一些考虑:

我使用单元格内容点击事件来处理复选框,所以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); } } } 

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

是的,您可以通过将DataGridViewCheckBoxCell转换为DataGridViewCheckBoxCell来完成此操作

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

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

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

使用单元格的ReadOnly属性来删除任何修改。

如果要将其置于不可见状态,则需要覆盖单元格的绘制代码。

我采取了spajce的答案并稍微调整一下以使其适用于我。

 for (var i = 0; i < datagridview1.Count; i++) { if ((bool)datagridview1[0, i]) { datagridview1[0, i] = new DataGridViewTextBoxCell { Style = { ForeColor = Color.Transparent, SelectionForeColor = Color.Transparent } }; } } 

我们基本上遍历行并查找“真实”复选框。 如果已经检查,那么我们将框转换为文本框并将其文本颜色设置为透明,以便单元格看起来是空的。 我希望这能帮助每个遇到这个问题的人,我花了好几个小时试图找到一个可行的答案。

有几种方法可以实现您想要的效果。

例如,您可以使用单元格的Readonly属性来避免用户更改值并将控件的外观更改为灰色:

C#DataGridViewCheckBoxColumn Hide / Gray-Out

一个简单而有效的替代方法是使用chkbox ThreeState属性。 在下面的代码中,如果我的对象没有电子邮件地址,那么我不想让用户勾选发送电子邮件的复选框。 为此,复选框值设置为Unknown and read-only,这意味着它显示为“unselectable”,用户无法修改。

 chk = DirectCast(row.Cells(m_idxEmailChecked), DataGridViewCheckBoxCell) If String.IsNullOrEmpty(contact.EmailAddress) Then chk.Value = enumTristate.Unknown chk.ReadOnly = True Else chk.ThreeState = False End If 

请注意,这还需要在复选框上设置多个关联的ThreeState属性。

  .ValueType = GetType(enumTristate) .TrueValue = enumTristate.True .FalseValue = enumTristate.False .IndeterminateValue = enumTristate.Unknown .ThreeState = True 

祝好运。

我试过查理的方式:

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

我得到的错误更少,但仍然不断得到它们。 所以我试图分别实例化新的DataGridViewTextBoxCell,这对我来说很有用(我也不需要设置复选框单元格值):

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

希望有人帮助!