我有以下代码:
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.DataBindingComplete
或dataGridView1.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;
}
}