Hide some datagridview checkbox cell

两盒软妹~` 提交于 2019-11-29 14:33:14

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

Assign a value to the checkbox cell. Then Convert it to a TextBox with a new value. Worked for me.

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

Use the cell's ReadOnly attribute to diable any modification.

If you want to turn it invisible, you need to override the painting code for the cells.

I took spajce's answer and tweaked it a bit to make it work for me.

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

We're basically iterating through the rows and looking for a 'true' checked box. If it's checked then we're converting the box to a text box and setting its text color to Transparent so the cell looks empty. I hope this helps everyone who had this problem, I spent hours trying to find a workable answer.

Charles Hays

Try to hide it and the value remains, which should prevent runtime errors.

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

There are several ways to accomplish what you want.

For example, you can use the Readonly property of the cell to avoid the user to change the values and change the appeareance of the control to look grayed:

C# DataGridViewCheckBoxColumn Hide/Gray-Out

A simple and effective alternative is to use the chkbox ThreeState property. In code below, if my object does not have an email address, then I don't want to allow the user to tick the checkbox for sending email. To do this, the check box value is set to Unknown and read-only, which means it is displayed as "unselectable" and user cannot modify.

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

Note this also requires several associated ThreeState properties to be set on the checkbox.

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

Good luck.

I tried Charlie's way:

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

I got fewer errors, but still kept getting them. So I tried to instantiate the new DataGridViewTextBoxCell separately, and that did the trick for me (I didn't need to set the checkbox cell value either):

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

Hope that helps someone!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!