C# DataGridViewCheckBoxColumn 隐藏/灰显
我有一个包含几列和几行数据的 DataGridView。其中一列是 DataGridViewCheckBoxColumn ,并且(基于行中的其他数据)我希望选择“隐藏”某些行中的复选框。我知道如何使其只读,但我希望它根本不显示,或者至少与其他复选框显示不同(变灰)。这可能吗?
I have a DataGridView
with several columns and several rows of data. One of the columns is a DataGridViewCheckBoxColumn
and (based on the other data in the row) I would like the option to "hide" the checkbox in some rows. I know how to make it read only but I would prefer it to not show up at all or at least display differently (grayed out) than the other checkboxes. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些解决方法:将其设为只读并将颜色更改回灰色。
对于一个特定的单元格:
或者,更好但更“复杂”的解决方案:
假设您有 2 列:第一列包含数字,第二列包含复选框,当 number > 时,该列不应可见。 2. 您可以处理
CellPainting
事件,仅绘制边框(例如背景)并中断其余绘制。为 DataGridView 添加事件CellPainting
(可以选择测试 DBNull 值以避免在空行中添加新数据时出现异常):它应该可以工作,但是如果您在检查条件的第一列中手动更改值,则必须刷新第二个单元格,因此添加另一个事件,例如
CellValueChanged
:Some workaround: make it read-only and change back color to gray.
For one specific cell:
Or, better but more "complicated" solution:
suppose you have 2 columns: first with number, second with checkbox, that should not be visible when number > 2. You can handle
CellPainting
event, paint only borders (and eg. background) and break painting of rest. Add eventCellPainting
for DataGridView (optionally test for DBNull value to avoid exception when adding new data in empty row):It should work, however if you change value manually in first column, where you check condition, you must refresh the second cell, so add another event like
CellValueChanged
:http://msdn.microsoft.com/en-us /library/system.windows.forms.datagridviewcheckboxcell.aspx
编辑:哦,等等,它是只读的。德普。
在这种情况下,请尝试用空的 DataGridViewTextBoxCell 替换单元格。
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell.aspx
Edit: Oh, wait, it's read only. Derp.
In which case, try replacing the cell with an empty DataGridViewTextBoxCell.
摘自自定义 Windows 窗体 DataGridView 控件中单元格的外观,您如果单元格处于只读模式,则可以捕获 CellPainting 事件并且不绘制单元格。例如:
唯一需要注意的是,当您更改
DataGridViewCheckBox
之一的ReadOnly
属性时,您需要调用dataGridView1.Invalidate();
细胞的。Taken from Customize the Appearance of Cells in the Windows Forms DataGridView Control, you could catch the CellPainting event and not draw the cell if its in read only mode. For example:
The only caveat is that you need to call
dataGridView1.Invalidate();
when you change theReadOnly
property of one of theDataGridViewCheckBox
cell's.