我在DataTable窗体上绑定了一个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; }
}
}一切都很好。我的问题是,细胞中显示的图像是闪烁的。有人知道为什么吗?
发布于 2015-02-24 06:43:10
正在发生闪烁,因为要在CellFormatting事件处理程序中设置图像。
根据MSDN,每次绘制每个单元格时都会发生CellFormatting事件,因此在处理此事件时应避免冗长的处理。
您可以根据需要处理DataBindingComplete或CellValueChanged事件来设置映像。
还可以通过为正在使用的实例创建自定义DoubleBuffering或通过反射为DataGridView启用DataGridView。
class CustomDataGridView : DataGridView
{
public CustomDataGridView()
{
DoubleBuffered = true;
}
}https://stackoverflow.com/questions/28689139
复制相似问题