DataTable Rowstate始终处于添加状态 [英] DataTable Rowstate always in Added state
问题描述
我有这些代码行
DataTable DTable = new DataTable();
DTable.Columns.Add("Domain");
DTable.Rows.Add("Sample");
DTable.AcceptChanges();
DGridView.DataSource = DomainTable;
此按钮用于保存更改
private void SaveDChangesButton_Click(object sender, EventArgs e)
{
foreach(DataRow dr in Dtable.Rows)
{
if(dr.RowState == DataRowState.Modified)
{
}
}
}
我的问题是,即使我修改了第一行, Rowstate
始终返回添加。
My problem is that even though I modified the first row, the Rowstate
always return Added.
推荐答案
RowState 是一个标志枚举,多个值可以打开。
RowState is a flags enumeration and more than one value can be on.
始终使用 HasFlag
函数来测试一个值!所以更改这个:
Always use the HasFlag
function to test for a value! So change this:
if(dr.RowState == DataRowState.Modified)
{
// do the saving stuff
}
为此:
if(dr.RowState.HasFlag(DataRowState.Modified) )
{
// do the saving stuff
}
请注意,对于添加
和修改
在 AcceptChanges
不能被调用..:
Do note that both for Added
and Modified
to be on AcceptChanges
must not have been called..:
已添加:该行已添加到DataRowCollection,
AcceptChanges尚未被调用
Added : The row has been added to a DataRowCollection, and AcceptChanges has not been called
已修改:行已被修改和AcceptChanges没有被调用
。
Modified : The row has been modified and AcceptChanges has not been called.
如果添加一行并让用户修改它在代码中)然后 标志应该 ,代码中的原始条件是 false ,尽管事实上已经修改!
If you add a row and let the user modify it (or do it in code) then both flags should be on and the original condition in your code is false despite the fact the has in fact been modified!
但是如果您有调用 AcceptChanges
for行或整个表格作为您的代码建议,然后既不标志开启!
But if you have called AcceptChanges
for the rows or the whole table as your code suggests then neither flag is on!
这篇关于DataTable Rowstate始终处于添加状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!