【问题标题】:Changing Background Color of DataGridview Cells更改 DataGridview 单元格的背景颜色
【发布时间】:2016-05-27 08:02:36
【问题描述】:

我正在使用 Win Forms 和 DataGridView 控件。我的数据网格视图未绑定到数据源,但添加了列和行。在某些情况下,我将网格视图设置为只读并尝试将每个单元格的背景颜色更改为灰色。但是灰色出现,然后又回到默认的白色。我不明白为什么会这样。

以下是数据网格视图上的事件

  1. 单元格结束编辑
  2. 编辑控件显示

使网格只读并设置背景颜色的代码

_dataGridView.ReadOnly=true;
foreach (DataGridViewRow row in _dataGridView.Rows)
        {
            row.DefaultCellStyle.BackColor = SystemColors.Control;

        }

请推荐

【问题讨论】:

    标签: winforms datagridview styles cell


    【解决方案1】:

    您可以捕获 Data Grid View 的 RowPostPaint 事件。

    在 VB.NEt 中它看起来像:

    Public Sub repainWithGrayColor() Handles DataGridView1.RowPostPaint
    If (condition) Then
       DataGridView1.Rows(e.RowIndex).Cells("ColumnName").Style.BackColor = Color.Gray
       DataGridView1.Rows(e.RowIndex).Cells("ColumnName").ReadOnly = True
     End If
    End Sub
    

    希望对您有所帮助。

    【讨论】:

    • 条件在gridview外触发。更改单选按钮时说。
    • 以这种方式手动更改属性是非常无效的,因为每次更改单个单元格时都会触发整个网格的重新绘制事件。我建议通过事件“CellFormating”进行样式更改
    猜你喜欢
    • 2013-04-12
    • 2015-09-29
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2014-02-27
    • 2011-09-24
    相关资源
    最近更新 更多