0

下面的代码 dr.SetAdded() 语句给出了这样的错误:

“setadded 和 setmodified 只能在 datarowstate 不变的数据行上调用” 现在该怎么办?

      dBindSet.AcceptChanges()


         For Each dt As DataTable In dBindSet.Tables

   BindingContext(dt).EndCurrentEdit()

          For Each dr As DataRow In dt.Rows

                    dr.SetAdded()

                    dr.Item("CREATEDON") = strServerDate
                    dr.Item("CREATEDBY") = iUserID

                 Next
            Next
4

2 回答 2

0

方法SetAdded更改RowState 为已添加。在 OP 中,您正在更新字段,因此您不必调用此方法。

如果您想调用此方法,请检查RowState.

For Each dr As DataRow In dt.Rows
   If dr.RowState=DataRowstate.Unchanged Then
      dr.SetAdded()
   End If
   dr.Item("CREATEDON") = strServerDate
   dr.Item("CREATEDBY") = iUserID
Next
于 2012-06-30T07:56:49.350 回答
0

如果我理解正确,您需要在表中已更改或插入的每一行中设置数据表的“CREATEDON”和“CREATEDBY”字段。如果是这种情况,您不应该调用AcceptChanges,因为这会将RowState每个 DataRow 的属性重置为Unchanged并且您将无法处理更改的行(并且数据库更新将失败)

' Don't call this, you will loose the RowState info....
'dBindSet.AcceptChanges()
For Each dt As DataTable In dBindSet.Tables
    ' Not really needed here. It serves to update the UI. 
    'BindingContext(dt).EndCurrentEdit()
    For Each dr As DataRow In dt.Rows
        if(dr.RowState = DataRowState.Added OrElse dr.RowState = DataRowState.Modified then            
            dr.Item("CREATEDON") = strServerDate
            dr.Item("CREATEDBY") = iUserID
        end if
    Next
Next
于 2012-06-30T12:09:53.057 回答