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次对话框 有什么办法实现相同的效果 但只弹一次?
现在的问题是点cancel也出现2次啊
if (MessageBox.Show("chenging", "message", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.OK)
你这里说的cancel是那个?是no?
if (MessageBox.Show("值已变更,是否修改?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
点No弹2次
if (MessageBox.Show("值已变更,是否修改?", "信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
点Cancel弹2次
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);
}
}
楼上的flag是一种方法,比较方便这样你的更新就分两种情况:一种是你打算要更新的,就是你愿意看到的
第二中是你不想更新的,就是在else代码中的这次更改