隐藏一些datagridview复选框单元格

时间:2013-01-02 14:46:35

标签: c# winforms datagridview datagridviewcheckboxcell

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

这是datagrid的屏幕:

enter image description here

我的问题是我需要禁用付费插件的复选框。在这种情况下,当“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事件了。它继承了,这就是为什么我试图避免使用它。

8 个答案:

答案 0 :(得分: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 = "";

答案 1 :(得分: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();
            }
        }

答案 2 :(得分:3)

使用单元格的ReadOnly属性禁用任何修改。

如果要将其变为不可见,则需要覆盖单元格的绘制代码。

答案 3 :(得分:2)

我接受了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 }
            };
        }
}

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

答案 4 :(得分:0)

有几种方法可以达到你想要的效果。

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

C# DataGridViewCheckBoxColumn Hide/Gray-Out

答案 5 :(得分:0)

一个简单而有效的替代方法是使用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
祝你好运。

答案 6 :(得分:0)

尝试隐藏它并保留值,这将防止运行时错误。

dataGridView1.Rows[row.Index].Cells[16].Style.Padding =
new Padding(dataGridView1.Rows[row.Index].Cells[16].OwningColumn.Width, 0, 0, 0);

答案 7 :(得分:-1)

我试过查理的方式:

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;

希望能帮助别人!

相关问题