当我尝试替换图像时程序挂起?

  • 本文关键字:程序 挂起 图像 替换 c#
  • 更新时间 :
  • 英文 :


我有以下代码:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.Columns[e.ColumnIndex].Name.Equals("edit"))
{
string status = dataGridView1.Rows[e.RowIndex].Cells["status"].Value.ToString();
if (status == "1") 
{        
dgv.Rows[e.RowIndex].Cells["edit"].Value = Properties.Resources.edit_disable;
}
}
}

当我尝试在此处替换图像时:

dgv.Rows[e.RowIndex].Cells["edit"].Value = Properties.Resources.edit_disable;

程序挂起和图像并呈现无限

您为更改图像选择了错误的事件。事件dataGridView1_CellFormatting在图像更改时触发,因此,如果使用此事件更改图像,则会进入无限循环。

由于您的代码正在查询单元格的Value,因此您可能希望切换到不同的事件,该事件在行/单元格数据更改或绑定时触发,例如DataGridView.DataBindingCompletedataGridView1.RowsAdded

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
string status = dataGridView1.Rows[e.RowIndex].Cells["status"].Value.ToString();
if (status == "1") 
{        
dgv.Rows[e.RowIndex].Cells["edit"].Value = Properties.Resources.edit_disable;
}
}

最新更新