c# - 禁用或灰显 DataGridView

标签 c# .net winforms datagridview

是否有任何简单的方法来禁用/灰显 DataGridView?比如在做的时候

dgv.Enabled = false

dgv 的外观没有改变。我看到有人附加了以下内容:

dgv.forecolor = gray
dgv.columnheader.forecolor = gray

但是,这看起来很笨拙。有没有更好的办法?

最佳答案

Private Sub DataGridView1_EnabledChanged(sender As Object, e As EventArgs) Handles DataGridView1.EnabledChanged
    If Not DataGridView1.Enabled Then
        DataGridView1.DefaultCellStyle.BackColor = SystemColors.Control
        DataGridView1.DefaultCellStyle.ForeColor = SystemColors.GrayText
        DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
        DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText
        DataGridView1.CurrentCell = Nothing
        DataGridView1.ReadOnly = True
        DataGridView1.EnableHeadersVisualStyles = False
    Else
        DataGridView1.DefaultCellStyle.BackColor = SystemColors.Window
        DataGridView1.DefaultCellStyle.ForeColor = SystemColors.ControlText
        DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window
        DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText
        DataGridView1.ReadOnly = False
        DataGridView1.EnableHeadersVisualStyles = True
    End If
End Sub

关于c# - 禁用或灰显 DataGridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8715459/

相关文章:

c# - 访问 Windows 窗体 C# 上的控件

c# - 如何从另一个子窗体关闭子窗体而不关闭主窗体c#

c# - 抽象类与接口(interface)的建议

c# - 运行代码片段并呈现结果的基本 VS 扩展(类似于 watch )

c# - 在代码隐藏中数据绑定(bind)到 objective-c LR 属性

.net - 从终端服务器 session 获取客户端名称的首选方式

c# - 处理 Entity Framework 核心中的并发条件以防止竞争条件

.net - 将 ADO.Net 数据服务应用程序推出到 IIS 时出现问题

c# - ManualResetEvent WaitOne(timeout) 提前返回。任何想法为什么?

c# - C#错误: The contextual keyword 'var' may only appear within a local variable declaration