我有一个DataGridView
与几列和几行数据。其中一列是DataGridViewCheckBoxColumn
和(基于行中的其他数据)我希望在某些行中“隐藏”复选框。我知道如何让它只读,但我宁愿它根本不显示,或至少显示不同(灰色)比其他复选框。这可能吗?C#DataGridViewCheckBoxColumn Hide/Gray-Out
4
A
回答
12
某些解决方法:将其设置为只读并将颜色更改为灰色。 对于一个特定的细胞:
dataGridView1.Rows[2].Cells[1].Style.BackColor = Color.LightGray;
dataGridView1.Rows[2].Cells[1].ReadOnly = true;
或者更好,但更“复杂”的解决方案:
假设你有2列:先用数量,第二,复选框,应该是不可见的,当数> 2。您可以处理CellPainting
事件,仅绘制边框(例如背景)并打破休息的绘制。添加事件CellPainting
为DataGridView中(可选测试的DBNull值,以避免异常增加的空行新数据时):
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//check only for cells of second column, except header
if ((e.ColumnIndex == 1) && (e.RowIndex > -1))
{
//make sure not a null value
if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != DBNull.Value)
{
//put condition when not to paint checkbox
if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value) > 2)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.Border | DataGridViewPaintParts.Background); //put what to draw
e.Handled = true; //skip rest of painting event
}
}
}
}
它应该工作,但是如果你在第一列,在那里你检查条件手动更改值,必须刷新第二单元,所以添加其他事件一样CellValueChanged
:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dataGridView1.InvalidateCell(1, e.RowIndex);
}
}
0
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell.aspx
DataGridViewCheckBoxCell.Visible = false;
编辑:哦,等等,它是只读的。 DERP。
在这种情况下,请尝试用空的DataGridViewTextBoxCell替换单元格。
0
从Customize the Appearance of Cells in the Windows Forms DataGridView Control两者,你可以赶上CellPainting事件,如果以只读模式不绘制细胞。例如:
public Form1()
{
InitializeComponent();
dataGridView1.CellPainting += new
DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}
private void dataGridView1_CellPainting(object sender,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
// Change 2 to be your checkbox column #
if (this.dataGridView1.Columns[2].Index == e.ColumnIndex && e.RowIndex >= 0)
{
// If its read only, dont draw it
if (dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly)
{
// You can change e.CellStyle.BackColor to Color.Gray for example
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
e.Handled = true;
}
}
}
}
唯一需要注意的是,你需要调用dataGridView1.Invalidate();
当你改变DataGridViewCheckBox
细胞的之一ReadOnly
财产。
相关问题
- 1. .NET datagridviewcheckboxcolumn
- 2. DataGridViewCheckBoxColumn窗口
- 3. DataGridViewButtonColumn充当DataGridViewCheckBoxColumn
- 4. DataGridViewCheckBoxColumn:布尔列上的FormatException
- 5. 绑定DataGridViewCheckBoxColumn没有更新绑定源
- 6. DataGridViewCheckBoxColumn更改复选框显示
- 7. 将DataGridViewCheckBoxColumn绑定到布尔值数组
- 8. C#编程创建datagridview - DataGridViewCheckBoxColumn cellValueChanged检查状态总是返回FALSE
- 9. 如何在DatagridviewCheckBoxColumn的Header中添加复选框控件?
- 10. 如何获取DataGridViewCheckBoxColumn以显示DataRelation状态?
- 11. 当第一个checkBox列被选中时获取两个DataGridViewCheckBoxColumn
- 12. WinForm应用程序:无法设置DataGridViewCheckBoxColumn的默认值
- 13. 有没有办法阻止DataGridViewCheckBoxColumn自动检查点击?
- 14. 为什么DatagridviewCheckboxColumn的复选框不被检查?
- 15. 手动生成的DataGridViewCheckBoxColumn不更新绑定对象
- 16. DatagridViewCheckBoxColumn将复选框左侧的文本定位到其后面的文本
- 17. 防止用户检查dataGridViewcheckBoxColumn如果选择了一个和存储特定的细胞含量取决于检查排
- 18. 从检查的DataGridView项目选择值
- 19. 在C#与C/C++
- 20. C C#
- 21. 排序为C,C++,C#或Objective-C
- 22. 将C-C重新绑定到C-c
- 23. 在Objective C中包装Objective C C++/C++
- 24. 处理.NET DataGridViewCheckBox更改事件
- 25. 将复选框添加到DataGridView并选中/取消选中它们
- 26. C/C++相当于C#System.Net.Mail
- 27. C/C++ - 在Visual Studio C++
- 28. Objective-C和MetroWerks C/C++ IDE
- 29. JSON响应C/C++对象和C/C++对象到Java/Objective C
- 30. C c有没有区别;和C c = C();?
'Visible'属性是只读的。 – john