如何使用CellValueChanged事件检索DataGridView单元格的先前值? [英] How can I retrieve the previous value of a DataGridView cell using the CellValueChanged event?

查看:438
本文介绍了如何使用CellValueChanged事件检索DataGridView单元格的先前值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用DataGridView的C#应用​​程序,每当用户更改其中的数据时,我都希望验证输入。

I am writing a C# application that uses a DataGridView and I would like to validate the input each time a user changes the data that's there.

我首先使用CellValidating事件,该事件具有一个非常不错的CancelEdit()方法,该方法会将单元格返回其先前的值。但是,无论用户是否离开单元格,无论事件是否更改,都会触发此事件。

I began by using the CellValidating event which has a really nice CancelEdit() method that will return the cell to its previous value. However, this event is fired every time the user leaves the cell, regardless of whether or not it has changed.

CellValueChanged是否支持某种取消或回滚方法,以前的值?这样,我仍然可以验证数据,但不会浪费时间使用不需要数据的单元,但是宁愿不牺牲在数据无效的情况下恢复单元的能力。

Does CellValueChanged support a sort of cancel or rollback method to the previous value? This way I would be able to still validate the data, but not waste time with cells that didn't need it, but would prefer not to sacrifice the ability to restore the cell if data is invalid.

下面是一段代码:

private void dataGrid1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if(dataGrid1.Columns[e.ColumnIndex].Name == "dateColumn")
    {
        String input = e.FormattedValue.ToString();

        // Split the date from MM/DD/YYYY format
        String[] temps = input.Split('/');
        try
        {
            if(temps[2].Length != 4)
                MessageBox.Show("The year entered is not the correct length.", "Invalid Year", MessageBoxButtons.OK);

            DateTime date = new DateTime(Convert.ToInt32(temps[2]), Convert.ToInt32(temps[0]), Convert.ToInt32(temps[1]));
        }
        catch (Exception ex) // If exception is thrown, date was invalid
        {
            MessageBox.Show("The date entered was invalid.", "Invalid date", MessageBoxButtons.OK);
            dataGrid1.CancelEdit(); // Set cell value back to what it was prior to user's change
            e.Cancel = true; // Have focus stays with this cell rather than move down a row.
        }
    }
}


推荐答案

您可以尝试这样的另一个方法:

You could try another aproach like this:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    newvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    oldvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}

假设它是一个int值,如果它的另一个数据类型也可以使用(除课程外)

Assuming its an int,if its another datatype then will also work(except offcourse the variables oldvalue and newvalue must be that type also).

或者通过您的问题,它只是关于旧值​​,那么您只需要CellBeginEdit事件,然后使用验证事件中的oldvalue变量。

Or by your question,its just about the old value,then you will only need the CellBeginEdit event and then use the oldvalue variable inside the validating event.

这篇关于如何使用CellValueChanged事件检索DataGridView单元格的先前值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆