C# 更改DataGridViewImageColumn中的图像值不会删除以前的图像
问题: 更改DataGridViewImageColumn单元格中显示的图像时,前一个图像仍位于新图像的后面: (注意绿色复选标记后面的红色错误) 我所拥有的: 该图像显示与计算机的连接状态。当机器状态更新时,将引发一个事件并更新映像 DataGridViewImageColumn的声明:C# 更改DataGridViewImageColumn中的图像值不会删除以前的图像,c#,winforms,datagridview,datagridviewimagecolumn,C#,Winforms,Datagridview,Datagridviewimagecolumn,问题: 更改DataGridViewImageColumn单元格中显示的图像时,前一个图像仍位于新图像的后面: (注意绿色复选标记后面的红色错误) 我所拥有的: 该图像显示与计算机的连接状态。当机器状态更新时,将引发一个事件并更新映像 DataGridViewImageColumn的声明: DataGridViewImageColumn imc = new DataGridViewImageColumn { HeaderText = "C
DataGridViewImageColumn imc = new DataGridViewImageColumn
{
HeaderText = "C$",
Name = "imc",
Width = 25,
ImageLayout = DataGridViewImageCellLayout.Stretch,
ValuesAreIcons = true
};
默认设置为:
sDGView.Columns["imc"].DefaultCellStyle.NullValue = null;
((DataGridViewImageCell)sDGView.Rows[0].Cells["imc"]).Value = null;
添加行时发生的事件:
private void SDGView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
((DataGridViewImageCell)sDGView.Rows[LastRow].Cells["imc"]).Value = null;
}
联机状态更改时调用的方法:
private void SetStatusImage(int ri)
{
var status = Core.Machines[ri].OnLine;
//sDGView.Rows[ri].Cells["imc"].Dispose();
var image = (DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"];
if (status is null)
{
image.Value = StatusImg[0];
}
else if (status is true)
{
image.Value = StatusImg[1];
}
else
{
image.Value = StatusImg[2];
}
}
图片:
public Icon[] StatusImg { get; private set; } = new Icon[]
{
Properties.Resources.Minus_Grey,
Properties.Resources.Tick_Green,
Properties.Resources.Error_Red
};
我所尝试的…
我已尝试将image.Value设置为null-无更改
我对单元格本身调用了Dispose()方法,然后创建了一个新的单元格来代替它,并将值设置为当前图像——前面的图像仍然显示在后面
似乎每次我更改单元格的值时,都会在顶部添加另一个图像。我可以通过观察程序的内存大小随着图像的变化而增加来进行验证。即使调用Dispose()方法,旧的方法实际上也不会消失
编辑:
我尝试让主窗体调用Refresh()方法,以防需要重新绘制。-不变
我尝试删除图像变量并直接设置图像:
private void SDGView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
((DataGridViewImageCell)sDGView.Rows[LastRow].Cells["imc"]).Value = StatusImg[0];
}
private void SetStatusImage(int ri)
{
var status = Core.Machines[ri].OnLine;
//sDGView.Rows[ri].Cells["imc"].Dispose();
//var image = (DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"];
if (status is null)
{
//image.Value = StatusImg[0];
((DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"]).Value = StatusImg[0];
}
else if (status is true)
{
//image.Value = StatusImg[1];
((DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"]).Value = StatusImg[1];
}
else
{
//image.Value = StatusImg[2];
((DataGridViewImageCell)sDGView.Rows[ri].Cells["imc"]).Value = StatusImg[2];
}
}
--相同的结果-前一个图像仍然保留在新选择的图像后面
(注意绿色复选标记后面的红色错误)我用以下简单代码测试了这种场景:
private void PlanningDayPlans_Load(object sender, EventArgs e)
{
DataGridViewRow r = new DataGridViewRow();
this.DataGridView1.Rows.Add(r);
}
private void Button1_Click(object sender, EventArgs e)
{
this.DataGridView1.Rows(0).Cells(0).Value = My.Resources.todo2;
}
private void Button2_Click(object sender, EventArgs e)
{
this.DataGridView1.Rows(0).Cells(0).Value = My.Resources.cross;
}
而且它的工作没有问题:
图片是透明背景的PNG(会显示问题),我可以来回切换,效果很好
这适用于未绑定和数据源
较少的场景
我想我知道你的问题可能是在哪里产生的。您可以设置
ValuesAreIcons
属性,该属性会影响背景的alpha通道,从而使图标“正确”。我会尝试使用此设置并使用PNG图标,这样就可以了。如何填充DataGridView
?它是分配给数据源的数据库中的数据表
?如果是,如果您在数据表中进行更改并更新数据源会怎么样?另外,您使用绑定吗?不-不使用数据绑定。我还加了一句“价值观”试图解决问题的行-但我明白你的意思-资源是作为图标而不是图像添加的,我做了更改,以便它们现在正确地存储为图像-将很快进行测试。就是这样-图像本身作为图标而不是图像存储在参考资料部分-进行更改解决了问题。另外发现它发生在我使用:ImageLayout=DataGridViewImageCellLayout.Stretch庆幸它有帮助:-)。我通常不会使用DataGridViewImageCellLayout.Stretch
,因为它会使图像变形。如果要填充单元格,通常需要使用Zoom
选项。Zoom正是我用来替换的。Stretch-它工作得非常完美。