首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WPF,DataGrid编者单元格数据为什么没有回滚

2013-09-05 
WPF,DataGrid编辑单元格数据为什么没有回滚private void datagrid1_CellEditEnding(object sender, DataGr

WPF,DataGrid编辑单元格数据为什么没有回滚
private void datagrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    string newValue = (e.EditingElement as TextBox).Text;
    int age;
    Int32.TryParse(newValue,out age);
    MessageBox.Show(age.ToString());
    if (age > 100)
        e.Cancel = true;
}

当提交单元格的编辑之后,判断年龄是否大于100,否则回滚。《WPF变成宝典》上说的,把Cancel设为true,就回滚了,但是为什么没有回滚呢?
[解决办法]
private bool cancelling;
private void datagrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (cancelling)
    {
        cancelling = false;
        return;
    }
    string newValue = (e.EditingElement as TextBox).Text;
    int age;
    Int32.TryParse(newValue,out age);
    MessageBox.Show(age.ToString());
    if (age > 100)
    {
        cancelling = true;
        DataGrid1.CancelEdit();
    }
}

热点排行