C# DataGridViewImageColumn,图像闪烁
我将DataGridView绑定到Windows窗体上的DataTable。我想在其中插入一个未绑定的DataGridViewImagecolumn,并根据另一列的值设置图像。图像是在DataGidView\u CellFormatting事件中设置的。代码如下C# DataGridViewImageColumn,图像闪烁,c#,datagridview,datagridviewimagecolumn,C#,Datagridview,Datagridviewimagecolumn,我将DataGridView绑定到Windows窗体上的DataTable。我想在其中插入一个未绑定的DataGridViewImagecolumn,并根据另一列的值设置图像。图像是在DataGidView\u CellFormatting事件中设置的。代码如下 DataGridView dgvResult = new DataGridView(); dgvResult.DataSource = dtResult; DataGridViewImageColumn imageColumn = ne
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; }
}
}
一切正常。我的问题是单元格中显示的图像在闪烁。有人知道为什么吗?之所以会出现闪烁,是因为您正在
CellFormatting
事件处理程序中设置图像
根据,每次绘制每个单元格时都会发生CellFormatting
事件,因此处理此事件时应避免冗长的处理
您可以根据需要通过处理数据绑定完成
或CellValueChanged
事件来设置映像
您还可以通过创建自定义的DataGridView
或通过正在使用的实例的反射,为DataGridView
启用DoubleBuffering
class CustomDataGridView : DataGridView
{
public CustomDataGridView()
{
DoubleBuffered = true;
}
}
闪烁?你能发布视频/gif吗?你是说iPhone的效果(当你拿着一个随机图标时)不像iPhone的图标一样闪烁吗。但感觉它每秒钟都在让人耳目一新。我不知道如何发布视频。哦,我明白了。我不确定这个问题。谢谢你的帮助。我将代码移动到RowsAdded事件。现在一切正常。通过将代码移动到RowsAdded事件,可以解决闪烁问题。但是当记录的数量更多时(比如200条记录),网格加载需要2到3秒。使用CellFormatting事件,它可以快速加载。@user1542352是否循环
DataGridView.Rows
以在RowsAdded事件中设置图像?是。对于(int i=e.RowIndex;i