DataTable.GetChanges()保持返回NULL [英] DataTable.GetChanges() keeps returning NULL

查看:522
本文介绍了DataTable.GetChanges()保持返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取 allData 中的所有行,但不能在 removeData

I am trying to get all the rows that exist in allData but not in removeData

public static DataTable RemoveDuplicateRows(DataTable allData, 
    DataTable removeData) 
{
    removeData.Merge(allData);
    DataTable newData = removeData.GetChanges(); 
    removeData.RejectChanges();
    return newData;
}

removeData 为空在这种情况下的调用(只是一个新的DataTable();)

removeData is empty prior to the call in this case (just a new DataTable();)

但是 newData 总是有一个值在 DataTable newData = removeData.GetChanges();

最终解决方案:

    public static DataTable RemoveDuplicateRows(DataTable allData, DataTable removeData) 
    {
        DataTable duplicate = allData.Clone();
        foreach (DataRow row in allData.Rows)
        {
            duplicate.ImportRow(row);
        }
        foreach (DataRow row in duplicate.Rows)
        {
            row.SetAdded();
        } 

        removeData.Merge(duplicate);
        DataTable newData = removeData.GetChanges(DataRowState.Added);
        removeData.RejectChanges();
        allData.RejectChanges();
        return newData;
    }


推荐答案

您的 removeData DataTable需要与 allData 具有相同的列/字段。换句话说,它不能只是一个新的DataTable()。

Your removeData DataTable needs to have the same columns/fields as allData. In other words, it can't just be a new DataTable().

这篇关于DataTable.GetChanges()保持返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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