private void dataGridView4_Click(object sender, EventArgs e)
        {
            strTemp = GetDGVValue();
        }
//datagridview值改变时如果用户点ok就直接改了  要是点cancel  让值返回原来的值          private void dataGridView4_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (MessageBox.Show("chenging", "message", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                MessageBox.Show("sucess");
                return;
            }
            else
            {
                dataGridView4.Rows[dataGridView4.CurrentCell.RowIndex].Cells[dataGridView4.CurrentCell.ColumnIndex].Value = strTemp;
                dataGridView4.Focus();
                dataGridView4.currentCell=this.dataGridView.rows[i].cell["columnname"]; 
                dataGridView4.beginEdit(false);
                return;
            }
}
但是弹出的是2次对话框 有什么办法实现相同的效果 但只弹一次?

解决方案 »

  1.   

    你自己多写了一行                MessageBox.Show("sucess"); 
      

  2.   

    成功当然返回了  
    现在的问题是点cancel也出现2次啊
      

  3.   

    不是等于yes,是ok,改成ok试试
    if (MessageBox.Show("chenging", "message", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.OK) 
      

  4.   


    你这里说的cancel是那个?是no?
      

  5.   

    我这里还是一样啊
    if (MessageBox.Show("值已变更,是否修改?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
    点No弹2次
    if (MessageBox.Show("值已变更,是否修改?", "信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
    点Cancel弹2次
      

  6.   

    你第一次改值弹了一次,你点Cancel,把值改了回去,又触发了一次dataGridView4_CellValueChanged事件,所以又弹了一次,可以设置一个flag来判断
      

  7.   

            bool self = false;
            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (self)
                {
                    self = false;
                    return;
                }
                if (MessageBox.Show("chenging", "message", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    MessageBox.Show("sucess");
                    return;
                }
                else
                {
                    self = true;
                    dataGridView1[e.ColumnIndex, e.RowIndex].Value = "haha";
                    dataGridView1.Focus();
                    dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dataGridView1.BeginEdit(false);
                }
            }
      

  8.   

    主要问题是你却笑时更新回去后再次出发了更新事件,所以会弹出两次,可以做个判断,就是在cancel的时候这次的更改不做弹出处理
    楼上的flag是一种方法,比较方便这样你的更新就分两种情况:一种是你打算要更新的,就是你愿意看到的
    第二中是你不想更新的,就是在else代码中的这次更改