我有一个显示贷款分期付款的datagridview
。我创建了一个datagridviewcheckbox
列,这样我就可以选择我想要支付的所有分期付款。
这是数据网格的屏幕:
我的问题是,我需要禁用付费安装的复选框。在这种情况下,当"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;
}
}
显然这不起作用,我得到了一个编译器错误消息,说明该属性是只读的。
有人知道如何将复选框单元格设置为不可见吗?
为了以防万一,我附加了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事件了。它是遗传的,这就是我尽量避免使用它的原因。
发布于 2013-01-02 22:53:47
可以,您可以通过将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();
}
}
发布于 2014-04-16 23:06:51
为复选框单元格赋值。然后将其转换为具有新值的TextBox。对我很管用。
dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";
发布于 2013-01-02 22:52:10
使用单元格的ReadOnly
属性禁用任何修改。
如果要将其转换为隐藏,则需要覆盖单元格的绘制代码。
https://stackoverflow.com/questions/14124033
复制相似问题