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

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

问题描述

我试图让所有存在于 ALLDATA 的行,但不是在 removeData

 公共静态数据表RemoveDuplicateRows(数据表ALLDATA,
    数据表removeData)
{
    removeData.Merge(ALLDATA);
    数据表newData = removeData.GetChanges();
    removeData.RejectChanges();
    返回newData;
}
 

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

newData 总是空的数据表newData = removeData.GetChanges后的值();

最终解决方案:

 公共静态数据表RemoveDuplicateRows(数据表ALLDATA,数据表removeData)
{
数据表重复= allData.Clone();
的foreach(DataRow的行allData.Rows)
{
duplicate.ImportRow(行);
}
的foreach(DataRow的行duplicate.Rows)
{
row.SetAdded();
}

removeData.Merge(一式两份);
数据表newData = removeData.GetChanges(DataRowState.Added);
removeData.RejectChanges();
allData.RejectChanges();
返回newData;
}
 

解决方案

removeData 数据表需要有相同的列/为 ALLDATA <字段/ code>。换句话说,它不能只是一个新的DataTable()。

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 is empty prior to the call in this case (just a new DataTable();)

But newData always has a value of null after the DataTable newData = removeData.GetChanges(); line

Final Solution:

	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;
	}

解决方案

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天全站免登陆