启用和禁用Datagridview中的单元格 [英] Enabling and disabling a cell in a Datagridview
本文介绍了启用和禁用Datagridview中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁可以告诉我怎么做?
解决方案
要禁用一个单元格,它必须是只读的,并且以某种方式变灰。此功能启用/禁用DataGridViewCell:
///< summary>
///在DataGridView中切换单元格的已启用状态。没有本地的
///支持禁用单元,因此需要这种方法。禁用状态
///表示单元格是只读的,并且显示为灰色。
///< / summary>
///< param name =dc>要启用/禁用的单元格< / param>
///< param name =enabled>单元格是启用还是禁用< / param>
private void enableCell(DataGridViewCell dc,bool enabled){
// toggle只读状态
dc.ReadOnly =!enabled;
如果(启用)
{
//将单元格样式恢复为默认值
dc.Style.BackColor = dc.OwningColumn.DefaultCellStyle.BackColor;
dc.Style.ForeColor = dc.OwningColumn.DefaultCellStyle.ForeColor;
}
else {
//灰色单元格
dc.Style.BackColor = Color.LightGray;
dc.Style.ForeColor = Color.DarkGray;
}
}
I am using a DataGridView control for displaying some data. I need to enable some data and disable some data dynamically based on some values in the grid.
Can anyone tell me how to do it?
解决方案
To "disable" a cell, it must be read-only and grayed out somehow. This function enables/disables a DataGridViewCell:
/// <summary>
/// Toggles the "enabled" status of a cell in a DataGridView. There is no native
/// support for disabling a cell, hence the need for this method. The disabled state
/// means that the cell is read-only and grayed out.
/// </summary>
/// <param name="dc">Cell to enable/disable</param>
/// <param name="enabled">Whether the cell is enabled or disabled</param>
private void enableCell(DataGridViewCell dc, bool enabled) {
//toggle read-only state
dc.ReadOnly = !enabled;
if (enabled)
{
//restore cell style to the default value
dc.Style.BackColor = dc.OwningColumn.DefaultCellStyle.BackColor;
dc.Style.ForeColor = dc.OwningColumn.DefaultCellStyle.ForeColor;
}
else {
//gray out the cell
dc.Style.BackColor = Color.LightGray;
dc.Style.ForeColor = Color.DarkGray;
}
}
这篇关于启用和禁用Datagridview中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文