2017-08-14 64 views
1

我有以下代码:当我尝试更换图像时程序挂起?

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; 

程序挂起和形象,呈现无限

+0

有你尝试删除并重新添加此资源? –

+0

是的,它是DataGridViewImageColumn – OPV

+0

通过代码来查看是否发生无限循环。在'dataGridView1_CellFormatting'事件改变'dgv.Rows [e.RowIndex] .Cells [ “编辑”]',它再次闪光'dataGridView1_CellFormatting',这改变'dgv.Rows [e.RowIndex] .Cells [ “编辑”]' ,无限广告。 –

回答

1

你选择了错误的事件改变图片。事件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; 
    } 
} 
+0

改为使用什么?当我需要检查每一行的值和格式列单元格? – OPV

+0

你可以分享一个小例子,我不明白清楚 – OPV

+0

这是你写的确切的代码,只有把它放在不同的事件方法。 –

相关问题