检查DataGridView中的值是否修改

We can register the events "CellBeginEdit", "CellValidating" and "CellEndEdit" at the same time to get the two values before and after the modification and compare them.

    string begin = "";
    string end = "";

    private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (dataGridView1.CurrentCell.Value == null)
        {
            begin = "";
        }
        else
        {
            begin = dataGridView1.CurrentCell.Value.ToString();
        }
    }

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        end = e.FormattedValue.ToString();
    }

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (begin != end)
        {
            MessageBox.Show("Value Changed!");
        }
    }
原文地址:https://www.cnblogs.com/jizhiqiliao/p/10490552.html