问题描述
有没有人遇到ds.hasChanges()是假的,尽管ds在断点检查时显然有变化?我一直在看它有一段时间,我看不到有什么问题...
// connectionstring and command has been set
DataSet ds = new DataSet();
BindingSource myBindingSource = new BindingSource();
SqlDataAdapter dataAdapter1 = new SqlDataAdapter();
dataAdapter1.Fill(ds,Data);
myBindingSource.DataSource = ds.Tables [Data];
//然后使用bindingnavigator
ds.HasChanges(DataRowState.Modified)对窗体上的datatable进行更改; //是false
现在,当我使用HasChanges在行之后设置断点并使用DataSet Visualizer时,我可以看到DataSet实际上已经改变了,但HasChanges仍然返回false。
我确定我错过了明显的...可以看到我有什么做错了?
干杯
尝试调用EndCurrentEdit在BindingContext第一:
DataTable dt = ds.Tables [Data];
this.BindingContext [dt] .EndCurrentEdit();
如果(ds.HasChanges(DataRowState.Modified))
{
//在这里做你的东西
}
还可以尝试调用 myBindingSource.EndEdit()
,将任何未提交的数据推送到 DataTable
。
Has anybody come across ds.hasChanges() being false despite that the ds clearly has the changes while you check it at a breakpoint?I've been looking at it for quite a while and I can't see what is wrong...
// connectionstring and command has been set
DataSet ds = new DataSet();
BindingSource myBindingSource = new BindingSource();
SqlDataAdapter dataAdapter1 = new SqlDataAdapter();
dataAdapter1.Fill(ds, "Data");
myBindingSource.DataSource = ds.Tables["Data"];
// then changes made to the datatable on a windows form using bindingnavigator
ds.HasChanges(DataRowState.Modified); // is false
Now when I set a breakpoint after the row with HasChanges and use DataSet Visualizer I can see that the DataSet has in fact changed, but HasChanges still returns false.
I'm sure I'm missing the obvious... can anybody see what I'm doing wrong?
Cheers
Try calling the EndCurrentEdit() on BindingContext first:
DataTable dt = ds.Tables["Data"];
this.BindingContext[dt].EndCurrentEdit();
if(ds.HasChanges(DataRowState.Modified))
{
// do your stuff here
}
Also try calling the myBindingSource.EndEdit()
that will push any un-commited data to the DataTable
.
这篇关于.NET DataSet.HasChanges错误地为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!