DataRow.RowState read-only, workaround

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top