【问题标题】:DataGridViewImageColumn, images flickeringDataGridViewImageColumn,图像闪烁
【发布时间】:2015-04-25 16:23:37
【问题描述】:

我有一个 DataGridView 绑定到我的 Windows 窗体上的一个 DataTable。我想向它插入一个未绑定的 DataGridViewImagecolumn 并根据另一列的值设置图像。图像在 DataGidView_CellFormating 事件中设置。代码如下

DataGridView dgvResult = new DataGridView();
dgvResult.DataSource = dtResult;
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn.Width = 40;
imageColumn.Name = "Image";
imageColumn.HeaderText = "";
dgvResult.Columns.Insert(0, imageColumn);

    private void dgvResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (dgvResult.Columns[e.ColumnIndex].Name == "Image")
        {
            DataGridViewRow row = dgvResult.Rows[e.RowIndex];
            if (Utils.isNumeric(row.Cells["CM_IsExport"].Value.ToString()) && Int32.Parse(row.Cells["CM_IsExport"].Value.ToString()) == 1)
            {
                    row.Cells["Image"].Value = Properties.Resources.export16;
            }
            else { row.Cells["Image"].Value = Properties.Resources.plain16; }
        }
    }

一切正常。我的问题是单元格中显示的图像闪烁。有人知道为什么吗?

【问题讨论】:

  • 闪烁?能发个视频/gif吗?你的意思是iPhone的效果吗(当你拿着一个随机图标时的那个)
  • 不像 iPhone 的图标闪烁。但感觉每一秒都在刷新。我不知道如何发布视频。
  • 哦,我明白了。不确定问题所在。

标签: c# datagridview datagridviewimagecolumn


【解决方案1】:

闪烁发生是因为您在CellFormatting 事件处理程序中设置图像。

根据MSDN,每次绘制每个单元格时都会发生CellFormatting事件,因此在处理此事件时应避免冗长的处理。

您可以根据需要通过处理DataBindingCompleteCellValueChanged 事件来设置图像。

您还可以通过创建自定义DataGridView 或通过反射您正在使用的实例为DataGridView 启用DoubleBuffering

class CustomDataGridView : DataGridView
{
    public CustomDataGridView()
    {
        DoubleBuffered = true;
    }
}

【讨论】:

  • 感谢您的帮助。我将代码移至 RowsAdded 事件。现在好了。
  • 闪烁问题通过将代码移动到 RowsAdded 事件来解决。但是当记录数较多时(比如 200 条记录),加载网格需要 2 到 3 秒。使用 CellFormating 事件,加载速度很快。
  • @user1542352 你是在循环DataGridView.Rows 以在 RowsAdded 事件中设置图像吗?
  • 是的。 for (int i = e.RowIndex; i
猜你喜欢
  • 2012-08-22
  • 2012-05-13
  • 2010-10-29
  • 1970-01-01
  • 2023-04-02
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多