dataGridView1.Rows[e.RowIndex].Cells[4].Value = int.Parse((dataGridView1[3, e.RowIndex].Value.ToString())) + int.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());
就是 单元格2=单元格2+单元格1
比如i=i+1;这里变成表格就成了死循环了呢?

解决方案 »

  1.   

    先把dataGridView1.Rows[e.RowIndex].Cells[4].Value的值用一个变量接收,然后再相加int i = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString()); dataGridView1.Rows[e.RowIndex].Cells[4].Value = int.Parse((dataGridView1[3, e.RowIndex].Value.ToString())) + i;
      

  2.   

           private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                  int i = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString()); 
                  dataGridView1.Rows[e.RowIndex].Cells[4].Value = int.Parse((dataGridView1[3, e.RowIndex].Value.ToString())) + i;
                }
            }代码就是有单元格数据发送改变(就是我重新录了值进某个单元格),就把当前行的:第4列=第4列+第3列的值,但是这里一直显示死循环。
      

  3.   

    或者加一个判断是哪一个单元格数据发生的改变,该单元格发生了改变才触发事件,否则就return
      

  4.   

    protected bool check=true;
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if(check)
                {
                   if (e.RowIndex >= 0)
                   {
                     int i = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());
                     dataGridView1.Rows[e.RowIndex].Cells[4].Value = int.Parse((dataGridView1[3, e.RowIndex].Value.ToString())) + i;
                     check=false;
                   }
                }
                else
                {check=true;}
            } 
      

  5.   

    if (e.RowIndex >= 0 && e.Colument!=4)
    {
    all right;
    }