C# 隐藏一些 datagridview 复选框单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14124033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide some datagridview checkbox cell
提问by Andres
I have a datagridview
showing installments of a loan. I created a datagridviewcheckbox
column so then I can select all the installments i want to pay for.
我有datagridview
显示分期付款的贷款。我创建了一个datagridviewcheckbox
列,这样我就可以选择我想支付的所有分期付款。
This is a screen of the datagrid:
这是数据网格的屏幕:
My issue is that I need to disable the checkboxes of the paid intallments. In this case, when "Restante" (what′s left to pay) is = 0
.
我的问题是我需要禁用已付费分期付款的复选框。在这种情况下,当“Restante”(还剩下什么)是= 0
.
I read some posts where they used the paint event to not show the checkbox cell, but i didnt like that solution. I thought of hiding the checkbox cell, but i don′t know if it is possible to do that.
我读了一些帖子,他们使用绘画事件来不显示复选框单元格,但我不喜欢那个解决方案。我想隐藏复选框单元格,但我不知道是否可以这样做。
Thats what i tried:
这就是我尝试过的:
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;
}
}
Obviously this does not works, I get a compiler error message saying that the property is read only.
显然这不起作用,我收到一条编译器错误消息,指出该属性是只读的。
Does somebody knows how to set the checkbox cell to invisible?
有人知道如何将复选框单元格设置为不可见吗?
Just in case, I attach the DataGridViewCheckboxColumn
creation code:
以防万一,我附上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;
EDIT:
编辑:
Some considerations:
一些考虑:
I use the cell content click event to handle the checkboxes, so readonly wont work. What I want is to hide the checkbox:
我使用单元格内容点击事件来处理复选框,所以 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);
}
}
}
In the other hand, I′m already using the Onpaint event. Its inherited, thats why I′m trying to avoid using it.
另一方面,我已经在使用 Onpaint 事件。它是继承而来的,这就是为什么我试图避免使用它。
采纳答案by spajce
Yes, you can do this by Converting the DataGridViewCheckBoxCell
to DataGridViewTextBoxCell
是的,你可以通过将做到这一点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();
}
}
回答by Matzi
Use the cell's ReadOnly
attribute to disable any modification.
使用单元格的ReadOnly
属性禁用任何修改。
If you want to turn it to hidden, you need to override the painting code for the cells.
如果要将其隐藏,则需要覆盖单元格的绘制代码。
回答by 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:
例如,您可以使用单元格的 Readonly 属性来避免用户更改值并将控件的外观更改为灰色:
回答by Charlie
Assign a value to the checkbox cell. Then Convert it to a TextBox with a new value. Worked for me.
为复选框单元格分配一个值。然后将其转换为具有新值的 TextBox。为我工作。
dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";
回答by Justin
I took spajce's answer and tweaked it a bit to make it work for me.
我接受了 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 }
};
}
}
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.
我们基本上是遍历行并寻找“真”复选框。如果它被选中,那么我们将把框转换为文本框并将其文本颜色设置为透明,这样单元格看起来是空的。我希望这可以帮助遇到这个问题的每个人,我花了几个小时试图找到一个可行的答案。
回答by Andrew Phillips
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,这对我有用(我也不需要设置复选框单元格值):
DataGridViewTextBoxCell c = new DataGridViewTextBoxCell();
c.Value = "";
dataGridView1.Rows[row.Index].Cells[16] = c;
Hope that helps someone!
希望对某人有所帮助!
回答by user3085342
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.
一个简单而有效的替代方法是使用 chkbox ThreeState 属性。在下面的代码中,如果我的对象没有电子邮件地址,那么我不想让用户勾选发送电子邮件的复选框。为此,复选框值设置为未知和只读,这意味着它显示为“不可选择”且用户无法修改。
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.
请注意,这还需要在复选框上设置几个关联的 ThreeState 属性。
.ValueType = GetType(enumTristate)
.TrueValue = enumTristate.True
.FalseValue = enumTristate.False
.IndeterminateValue = enumTristate.Unknown
.ThreeState = True
Good luck.
祝你好运。
回答by 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);