C
Craig Yellick
I've read many postings expressing frustration that the RowState
property of a DataRow object is readonly. Here's a workaround that
works for me. Below, the function adds a new row to a datatable and
sets the rowstate as specified. You can take the general idea and
adapt it to your needs. The key to this solution is realizing that
individual rows in the table have the .AcceptChanges method.
private Function AddNewRow( _
ByVal dt As DataTable, _
ByVal rowState As DataRowState, _
ByVal rowData() as object) As DataRow
Dim row As DataRow
row = dt.Rows.Add(rowData)
Select Case rowState
Case DataRowState.Added
'do nothing, default state is Added
Case DataRowState.Unchanged
row.AcceptChanges()
Case DataRowState.Deleted
row.AcceptChanges()
row.Delete()
Case DataRowState.Modified
row.AcceptChanges()
row.BeginEdit()
row.EndEdit()
Case Else
Throw New Exception("Unsupported rowstate")
End Select
Return row
End Function
-- Craig Yellick, Alto
property of a DataRow object is readonly. Here's a workaround that
works for me. Below, the function adds a new row to a datatable and
sets the rowstate as specified. You can take the general idea and
adapt it to your needs. The key to this solution is realizing that
individual rows in the table have the .AcceptChanges method.
private Function AddNewRow( _
ByVal dt As DataTable, _
ByVal rowState As DataRowState, _
ByVal rowData() as object) As DataRow
Dim row As DataRow
row = dt.Rows.Add(rowData)
Select Case rowState
Case DataRowState.Added
'do nothing, default state is Added
Case DataRowState.Unchanged
row.AcceptChanges()
Case DataRowState.Deleted
row.AcceptChanges()
row.Delete()
Case DataRowState.Modified
row.AcceptChanges()
row.BeginEdit()
row.EndEdit()
Case Else
Throw New Exception("Unsupported rowstate")
End Select
Return row
End Function
-- Craig Yellick, Alto