在 Datagridview 中启用和禁用单元格 [英] Enabling and disabling a cell in a Datagridview

查看:22
本文介绍了在 Datagridview 中启用和禁用单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DataGridView 控件来显示一些数据.我需要根据网格中的某些值动态启用某些数据并禁用某些数据.

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.

谁能告诉我怎么做?

推荐答案

要禁用"一个单元格,它必须是只读的并且以某种方式变灰.此函数启用/禁用 DataGridViewCell:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆